How to remove and insert entity in same transaction?
posted 6 years ago
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?