File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to remove and insert entity in same transaction? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to remove and insert entity in same transaction?" Watch "How to remove and insert entity in same transaction?" New topic
Author

How to remove and insert entity in same transaction?

hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44
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?

Thanks

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove and insert entity in same transaction?