JPA update doesn't work in a many-to-many relationship -
i have 3 tables:
customer(idcustomer,...)
is_managed(idcustomer,idperson)
sales_person(idperson,...)
there @manytomany
relation between customer
, sales_person
.
when perform delete, works fine: customer
, is_managed
, sales_person
deleted.
but when perform update, customer
, is_managed
updated, sales_person
not.
for example, if update customer
deleting sales_person
, it's deleted in is_managed
table, not in sales_person
table.
how caused , how can solve it?
here's relevant code:
// update customer public string updatecustomer(customer customer,arraylist<sales_person> sales_persons,arraylist<involved_group_relation> involved_groups, macro_market macro_market)throws ioexception { // insert sales_person attached customer arraylist<sales_person> sales_personc = new arraylist<sales_person>(); sales_personc.addall(sales_persons); customer.setsalespersons_belongto(sales_personc); // insert in customer sales_persons em.merge(customer); return customer.getnamecustomer(); } // entity customer @entity @table(name="customer") public class customer implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue( strategy = generationtype.identity) private long idcustomer; private string titletypeaccount; private string namecustomer; /** relations **/ // customer - sales_person @manytomany( cascade={cascadetype.all} ) @jointable( name="is_managed", joincolumns=@joincolumn(name="idcustomer"), inversejoincolumns=@joincolumn(name="idperson") ) private collection<sales_person> salespersons_belongto; ... ... // entity sales_person @entity @table(name="sales_person") public class sales_person implements serializable { private static final long serialversionuid = 1l; @id private long idperson; private string namesalesperson; private string jobfunction; private string titleorganization; @manytomany( mappedby="salespersons_belongto" ) private collection<customer> customers; ... ...
if want salesperson deleted database, must call em.remove() on salesperson. calling merge() not enough.
also, if want salesperson's changes merged, must call merge() on salesperson well, or set cascade merge on salespersons_belongto relationship.
also ensure maintaining both sides of bi-directional relationship, not 1 side.
Comments
Post a Comment