java - JPA does not delete database row -


i'm facing delete problem jpa, , code:

public deleteline(int idline) {   line line = em.find(line.class,idline);   header header = line.getheader();   this.deleteline(header,line); }  public boolean deleteline(header header, line line) {     try {       line.setidarticle(null);       line.setdetail(deleted_tag);       line.setquantity(0.0f);       em.merge(line);       header.getlinecollection().remove(line);       em.remove(line);       em.flush();     }     catch (exception ex) {       log.log(level.severe, null, ex);     }   return true; } 

when call deleteline(), end database row idarticle being null, details equal deleted_tag constant , quantity equal 0. row still there, despite em.remove.

i've tried add line.setheader(null) before removal, constraintviolationexception because header field cannot null.

obviously i'm doing wrong, cannot figure out what.

there's entities code:

public class header implements serializable {   [...]   @onetomany(cascade = cascadetype.all, mappedby = "header")   private collection<line> linecollection;   [...] }  public class line implements serializable {   [...]   @joincolumn(name = "header", referencedcolumnname = "header")   @manytoone(optional = false)   private header header;   [...] } 

i've done tests deleting row via jpql (delete linea idlinea=?), , deletes row. then, when jpa commit, line reappears due insert done jpa.

is there way discover why? obviously, there's entity generates insert, i'm removing line form header, can figure out triggering insert?

i figured out problem was: when user deletes row, doing this

  • call deleteline
  • do calculations , update header results

due how jpa works, modifications header fields force jpa skip deleting child (because of cascade all) or reinserting it, because in fact, entity still exists , hibernate "walks" , reconstructs parent-childs tree.

for people facing same problem, correct way i've found if want delete child , update parent after deletion is:

  • remove child parent collection
  • update header
  • delete child

for me, doing thing in exact order solved problem.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -