File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes JPA - EntityExistsException with Composite Id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA - EntityExistsException with Composite Id" Watch "JPA - EntityExistsException with Composite Id" New topic
Author

JPA - EntityExistsException with Composite Id

Rodrigo Sasaki
Greenhorn

Joined: Mar 09, 2012
Posts: 24
Hello everyone.

I am having a problem with JPA that I can't understand why. Here's the scenario.

I have my entity called RecipeVersion, that has a composite Id, that is composed by

recipeVersionId | recipeId | storeId

There is a business rule, that whenever a recipe version is already associated with a product, it cannot be altered, if the user tries to alter that version, a new version is persisted with his alterations, leaving the original one unchanged.

so when I get on this specific case, I make a copy of the object, so I don't have any problem with the references and add 1 to the value of the version by code.

if the PK is:

recipeVersionId = 1
recipeId = 32
storeId = 1


I search the database for the highest version of that recipe, and add 1, so in this case, if there is only that 1 recipe, the new PK looks like this:

recipeVersionId = 2
recipeId = 32
storeId = 1


But when I try to persist this new object, an EntityExistsException is thrown.

javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session:
[com.model.scm.RecipeVersionVO#com.model.scm.RecipeVersionPK@1845a]


My Composite Id is mapped with @IdClass Annotation.

Does anybody have any ideas on what this might be?

Or any suggestions, I heared some people say this may happen because I try to control the recipeVersionId by code, is there any other way I could do it?

Please let me know if you need me to post some code or configuration, for a more clear view.

Thanks!

EDIT:PS: I have already tried detaching the object I create my copy from, using entityManager.detach but that still didn't work

EDIT 2: Here are the entities


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA - EntityExistsException with Composite Id
 
Similar Threads
JPA-Hibernate - EntityExistsException Question
EntityExistsException
JPA-Hibernate - EntityExistsException Question
EntityExistsException how to know which key i duplicate?
EntityExistsException