0

I have a simple represent of datatables grid, every datatable is a entity. Every root datatable have inner collection of entities (@OneToMany). When I'm trying to delete a root entity, everything goes well. But when I'm trying to delete inner Entity, nothing happenes. Updating works well. So, my root entity is Group and inner Task

Group entity:

@Entity
@Table(name = "groups")
@NamedQueries({
        @NamedQuery(name = Group.GET_ALL_GROUPS_BY_USER_ID,query = "SELECT g FROM Group g WHERE g.user.id = :user_id")
})
public class Group implements Serializable {

    public static final String GET_ALL_GROUPS_BY_USER_ID = "GetAllGroupsByUserID";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @OneToMany(fetch = FetchType.EAGER,mappedBy = "group",cascade = {CascadeType.PERSIST,CascadeType.REMOVE})
    private List<Task> tasks;


}

Task entity:

@Entity
@Table(name = "tasks")
public class Task implements Serializable {

    private static final long serialVersionUID = -5320541404604330486L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String text;
    private boolean isComplete;
    @ManyToOne
    @JoinColumn(name = "group_id")
    private Group group;


    @PreRemove
    public void remove(){
        group = null;
        System.out.println(">>>REMOVING<<<");
    }


}

Task Manager:

@Stateless
public class TaskManager {
    @PersistenceContext(unitName = "MySqlPU")
    EntityManager em;

    public Task update(Task task){
        return em.merge(task);
    }

    public void delete(Task task){
        task = em.merge(task);
        em.remove(task);
        System.out.println(">>>DELETED!!!<<<");
    }

}

Group Manager:

@Stateless
public class GroupManager {

    @PersistenceContext(unitName = "MySqlPU")
    EntityManager em;


    public List<Group> getAllGroupsByID(Long id){
        return em.createNamedQuery(Group.GET_ALL_GROUPS_BY_USER_ID,Group.class)
                .setParameter("user_id",id)
                .getResultList();
    }



    public void delete(Group group){
        group = em.merge(group);
        em.remove(group);
    }
}

Thank you in advance!

UPD:

As @Chris said, I solved problem by detaching Task entity from Group's collection. Now this works:

public void delete(Task task){
    task = em.merge(task);
    Group group = em.merge(task.getGroup());
    group.getTasks().remove(task);
    em.remove(task);
}

Thank you all!

Max Max
  • 389
  • 1
  • 2
  • 12

1 Answers1

0

You need to remove any references to the task before it can be deleted. Any existing references that use cascade merge or persist that are not cleaned up can cause the entity to be re-persisted, undoing your delete.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • I did it already in my Task entity by @PreRemove annotation – Max Max Mar 10 '16 at 16:06
  • This nulls out the task's reference to group. The issue is with any reference to task - you left group referencing this task, which because the reference is cascade merge, forces the task to be resurrected. – Chris Mar 10 '16 at 16:30