Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to remove and insert entity in same transaction?  RSS feed

Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement in which I create some relationship between two object from GUI. So I have a list grid kind of thing where I can add multiple objects to top object in page.
Now say there was a relationship already from A to B and A to C and A to D for A object.
I go and delete A to B from gui and again add B .
Now I can perform all three actions at same go. Update , remove and add new relationship and save them.
I pass 3 lists of new, updated and removed relationship to my service bean and that bean in turn calls DAO methods in loop to remove, add or update the relationships.
My EntityManager is at DAO level.
There is a unique constraint which on ids of two objects (A and B in this case) at database table level .
Now what happens is that aafter all 3 actions when hibenrate tries to commit I get following exception.

So I believe some how even though i call getEntitymanager.remove(eObject) before getEntityManager.saveorupdate(eObj) , some how it is not geting cleared in 1st level cache and so I get this exception.
So can any one help how can I achieve removing and inserting same entity in a transaction in JPA / Hibernate?


With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!