This question is similar to this one but the person asking never confirmed if it worked. entityManager.persist(user) -> javax.persistence.EntityExistsException: User@b3089 is already persistent
Scenario
ProductCategory has a OneToMany
relationship with Account which inversely has a ManyToOne
relationship wuth ProductCategory
. When ProductCategory
is inserted, Accounts are not available. So ProductCategory
is inserted without accounts. Later when accounts are available, I would like to insert accounts in the accounts table and also update ProductCategory with Accounts. The issue is with updating accounts in ProducCategory. When I use mgr.persist for productCategory, I get a error Entity already is Persistent!. When I do not use persist (as per suggestion the link, provider(datanucleus) will take care of writing it to the database at commit time), it does not update. The entities and methods are as 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 Account {
@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....");
Account 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();
try {
if (!containsProductCategory(productcategory)) {
throw new EntityNotFoundException("Object does not exist");
}
mgr.persist(productcategory);
} finally {
mgr.close();
}
return productcategory;
}
**If I uncomment new `ProductCategoryEndpoint().updateProductCategory(pc)` I get the error Entity already persistent.
If I keep it commented, the account is not updated in ProductCategory
**