hibernate - Spring JPA - deleting child element doesn't reflect in database table -
i'm having problems deleting child element of one-to-many relationship entity. here's code snippet:
@override @transactional public void deletetask(uuid listid, uuid taskid) { tasklist list = repo.findone(listid); system.out.println("old list: " + list); for(task t : list.gettasks()) { if(t.getid().tostring().equals(taskid.tostring())) { system.out.println(list.gettasks().remove(t)); system.out.println("task id " + taskid + " deleted."); } } system.out.println("new list: " + repo.save(list)); } the task class this:
@entity(name = "task") public class task implements serializable { // id , 3 fields @manytoone @joincolumn(name="tasklist_id") private tasklist parentlist; // 3 more fields // constructor public task() {} //getters , setters } and tasklist class this:
@entity(name = "task_list") public class tasklist implements serializable { // id , 2 fields @onetomany(mappedby="parentlist", cascade={cascadetype.all}) private list<task> tasks; // constructor public tasklist() {} } the task entity child, , though save() function returns truncated tasklist, can't changes show in separate query database. number of tasks remains same. however, deleting list through repo.delete(listid) works fine both list , tasks gone.
here, repo repository corresponding parent tasklist class. operations child task class happen through @onetomany({cascade=cascadetype.all}) relation.
for reason, searching tasklists using repo.findall() returns faulty results.
i'm doing fundamentally wrong. please tell me do.
you need add orphanremoval = true mapping:
@onetomany(mappedby="parentlist", cascade={cascadetype.all}, orphanremoval=true)
list.gettasks().remove(t) removes entity collection, need tell jpa remove db. done orphanremoval attribute.
Comments
Post a Comment