This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JPA - EntityExistsException with Composite Id

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
[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


 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic