Hibernate EntityManager.remove(entity) deletes record from database and association but not from entity collection
<p>So, i have 2 entities <strong>Project</strong> &amp; <strong>TaskPlan</strong>, inside Project entity i have a <strong>@ManyToMany</strong> association with the <strong>TaskPlan</strong> entity</p>

<pre><code>@Entity
@Table(name = "project")
public class Project {     
@Column(name = "descriptions")
private String descriptions;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "project_has_task_plan",
        joinColumns = {@JoinColumn(name = "project_id")},
        inverseJoinColumns = {@JoinColumn(name = "task_plan_id")})
private Set&lt;TaskPlan&gt; taskPlans;

public Set&lt;TaskPlan&gt; getTaskPlans() {
    return taskPlans;
}

public void setTaskPlans(Set&lt;TaskPlan&gt; taskPlans) {
    this.taskPlans = taskPlans;
}

....
@Entity
@Table(name = "task_plan")
public class TaskPlan {

@GeneratedValue(generator = "idIncrementor")
@GenericGenerator(name = "idIncrementor", strategy = "increment")
@Id
private Long id;

@Column(name = "name")
private String name;

@Column(name = "description")
private String description;...
</code></pre>

<p>I am using DAO methods to interract with both entities and i have a <strong>@PersitenceContext(type = PersistenceContextType.EXTENDED)</strong>.I am using generics also to get entity by id</p>

<pre><code>  public T getById (Long id) {
    if (id==null) {
        return null;

    } else {

        Query query = this.entityManager.createQuery("from " + type.getName() + " where id=:id");
        query.setParameter("id", id);
        List&lt;T&gt; result = query.getResultList();
        if (!result.isEmpty()) {
                return result.get(0);

        }

        throw new IllegalArgumentException("Not found!");
    }

}
</code></pre>

<p>I am updating taskplans collection on Project entity using the following method and adding a new element to <strong>taskPlans</strong> Set</p>

<pre><code>//ProjectService class
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseBody
public void updateProject(@RequestBody Project project) throws Exception {
    this.projectFacade.updateOne(project);
}
 //ProjectFacade class
@Override
public void updateOne(Project project) {
    Project itemFromDbs = this.projectDao.getById(project.getId());
    if (itemFromDbs != null) {
        itemFromDbs.setName(project.getName());
        itemFromDbs.setDescriptions(project.getDescriptions());
        if (project.getTaskPlans() != null) {
            project.getTaskPlans().stream().forEach(taskPlan -&gt; {
                TaskPlan taskPlanToAdd = taskplanDao.getById(taskPlan.getId());
                if (taskPlanToAdd == null) {
                    itemFromDbs.getTaskPlans().add(taskPlan);
                }
            });
        }
        this.projectDao.updateProject(itemFromDbs);
    }
}
....
 //ProjectDao class
 @Transactional
public void updateProject(Project project) {
    Project itemFromDbs = this.getById(project.getId());
    if (itemFromDbs != null) {
        itemFromDbs.setTaskPlans(project.getTaskPlans());
        entityManager.getEntityManagerFactory().getCache().evictAll();
        entityManager.persist(itemFromDbs);

        entityManager.flush();
        entityManager.clear();
    }
}
</code></pre>

<p>Everything works fine for the moment, the bad thing comes when i try to remove a taskplan from <strong>TaskPlan</strong> entity..<strong>.entitymanager.remove()</strong> works fine, the entity gets removed from the <strong>TaskPlan table</strong> and the associated <strong>project_has_task_plan</strong> table, but when i refresh the page and <strong>getById</strong> on <strong>Project</strong> entity gets triggerd the deleted item from <strong>TaskPlan</strong> entity is still inside <strong>Set taskplans</strong>...</p>

<p>I have to mention that <strong>second-level cache is completly disabled</strong> and <strong>clear()</strong> or <strong>evictAll()</strong> methods does not work.</p>

<p>Here is the removal code:</p>

<pre><code> //TaskPlanService class
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
@ResponseBody
public void deleteTaskPlan(@PathVariable("id") Long taskPlanId) throws Exception {
    this.taskplanFacade.deleteOne(taskPlanId);
}

 //TaskPlanFacade class
 public void deleteOne(Long taskPlanId) {
    taskplanDao.deleteById(taskPlanId);
}
 //TaskPlanDao class
@Transactional
@Override
public void deleteById(Long taskPlanId) {
    TaskPlan taskPlanFromDbs=this.getById(taskPlanId);
    if(taskPlanFromDbs!=null){
        taskPlanFromDbs.getTasks().stream().forEach(task -&gt; {//this things
                      //are associated with the                            
                     //taskplan entity but for the moment i don't interract   
                    //with them
            task.getBadges().stream().forEach(badge -&gt; {
                entityManager.remove(badge);
            });
            entityManager.remove(task);
        });
        taskPlanFromDbs.getBadges().stream().forEach(badge -&gt; {
            entityManager.remove(badge);
        });
        entityManager.remove(taskPlanFromDbs);
        entityManager.flush();

        entityManager.clear();

    }
}
</code></pre>