• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic