Sunday, 15 August 2010

java - JPA - Entity is already persistent - using GAE -



java - JPA - Entity is already persistent - using GAE -

this question similar 1 person asking never confirmed if worked. entitymanager.persist(user) -> javax.persistence.entityexistsexception: user@b3089 persistent

scenario productcategory has onetomany relationship account inversely has manytoone relationship wuth productcategory. when productcategory inserted, accounts not available. productcategory inserted without accounts. later when accounts available, insert accounts in accounts table , update productcategory accounts. issue updating accounts in produccategory. when utilize mgr.persist productcategory, error entity persistent!. when not utilize persist (as per suggestion link, provider(datanucleus) take care of writing database @ commit time), not update. entities , methods follows:

@entity public class productcategory { @id @column(name = "cat_id", allowsnull="false") @generatedvalue(strategy = generationtype.identity) private key catid; @column(name = "cat_short_name", length=30) private string catshortname; //other fields @onetomany(mappedby="productcategory",targetentity=account.class, fetch=fetchtype.eager, cascade=cascadetype.all) private arraylist<account> accounts; //getters & setters @entity public class business relationship { @id @column(name = "acct_nbr_key", allowsnull="false") @generatedvalue(strategy = generationtype.identity) private key acctnbrkey; @column(name = "cat_id") private key acctcatid; //other fields @manytoone(optional=false, fetch=fetchtype.eager, cascade=cascadetype.all) @joincolumn(name="cat_id", insertable=false, updatable=false) private productcategory productcategory; //getters & setters accountendpoint.java public void insertaccountbulk() { log.info("accountendpoint.insertaccountbulk...."); business relationship account = new account(); productcategory pc = (new productcategoryendpoint()).getproductcategorybyshortname("savings"); account.setproductcategory(pc); account.setacctcatid(pc.getcatid()); //setting other fields //updationg accounts in product category getentitymanager().detach(pc); if(pc.getaccounts() == null){ arraylist<account> accts = new arraylist<account>(); accts.add(account); pc.setaccounts(accts); } else{ pc.getaccounts().add(account); } getentitymanager().merge(pc); **//new productcategoryendpoint().updateproductcategory(pc);**

productcategoryendpoint.java

@apimethod(name = "updateproductcategory") public productcategory updateproductcategory(productcategory productcategory) { entitymanager mgr = getentitymanager(); seek { if (!containsproductcategory(productcategory)) { throw new entitynotfoundexception("object not exist"); } mgr.persist(productcategory); } { mgr.close(); } homecoming productcategory; } **if uncomment new `productcategoryendpoint().updateproductcategory(pc)` error entity persistent.

if maintain commented, business relationship not updated in productcategory**

judging other question you're using gae/datastore not rdbms, , not using hibernate - please update question , remove tag.

about problem, if entity persistent trying persist object particular key when object key exists (i.e passed transient object persist. should pass detached object method. log tell state objects in

java android google-app-engine jpa

No comments:

Post a Comment