Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to remove and insert entity in same transaction?

Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


    Bookmark Topic Watch Topic
  • New Topic