java - ObjectDeletedException when i move items from one bag to another -


i have invoice object has several invoice items. based on user selection need move of items 1 invoice another. using mysql , hibernate.

invoice items defined lazy loaded follows:

in java following:

invoice newinvoice = new invoice(); iter = invoice.getpurchasableitems().iterator(); while(iter.hasnext()){ // item not purchased new invoice     invoiceitem item = iter.next();     invoice.getitems().remove(item);     newinvoice.additem(item); } 

then save 2 invoices:

iter = newinvoice.getitems().iterator(); while(iter.hasnext()){      invoiceitem item = iter.next();     session.saveorupdate(item); } iter = invoice.getitems().iterator();     while(iter.hasnext()){     invoiceitem item = iter.next();     session.saveorupdate(item); } session.commit(); 

and objectdeletedexception. not believe have other reference invoice items other moving them 1 invoice other. because want make sure not ended lot of un-used ids invoice item table.

can suggest way fix problem?

maybe has nothing problem first while modifying collection iterating. should use remove() method iterator.

then changing invoiceitem 1 bag not enough need update invoiceitem, don't know if have manytomany on manytoone in invoiceitem, think have set both side of relation.

invoice newinvoice = new invoice(); iter = invoice.getpurchasableitems().iterator(); while(iter.hasnext()){ // item not purchased new invoice     invoiceitem item = iter.next();     iter.remove();     item.setinvoice(newinvoice);     newinvoice.additem(item); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -