• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA - EntityExistsException with Composite Id  RSS feed

Rodrigo Sasaki
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

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.


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

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!