Forums Register Login

JPA Delete Entity and Refresh Collections containing Entity

+Pie Number of slices to send: Send
What is the best way to delete an entity and then refresh all of the places in the application that have a reference to it? I have two references to a particular entity and when I delete the entity I want the other location updated. Here is the scenario:

Entity A has a collection (List) of Entity B (OneToMany).

I obtain an instance of Entity A via a first query.
I obtain an instance of Entity B via a second query.
I delete the instance of Entity B which was obtained from the second query (EntityManager.remove).
Now, the collection of Entity B in the instance of entity A is not correct (it should contain one less instance of Entity B).

How do I update the collection in entity A?

I've tried using EntityManager.refresh on Entity A but the collection is not refreshed and iterating over the collection results in an exception:

javax.persistence.EntityNotFoundException: No row with the given identifier exists



I've also tried requerying Entity A, but the collection is not refreshed (I suppose it continues to use a cached copy).

Do I need to remove the object from the collection directly (List.remove)? I would have thought that the refresh would have done it, especially since I have (cascade = CascadeType.ALL) set on the OneToMany relationship annotation.

Note: If I add a new Entity B then a EntityManager.refresh on Entity A DOES work.
+Pie Number of slices to send: Send
Are you refreshing before you commit the transaction or after?

If before the delete may not be flushed yet, try flushing first.

In general you should remove all references to an object before deleting it, not rely on refresh. If the relationship has a constraint then the delete would fail if you did not remove the reference.
+Pie Number of slices to send: Send
Thanks for your response. To answer your question: I am removing and then flushing:


I'm using a stateful session bean and calling the removeEntity method using an expression language method expression from a JSF page. I'm obtaining a new reference to the entity before removing because the entity that is being passed into the method is detached (I think). I tried to use the merge method instead of getReference but if I do then when I call remove I get an exception:

javax.persistence.EntityNotFoundException: deleted entity passed to persist



This might be caused by a cascade rule telling JPA to do something clever behind my back.

Is there a way to tell JPA to clear the cache? I especially don't like the fact that if I requery entity A after I delete entity B JPA still has entity B in the list associated with entity A!
+Pie Number of slices to send: Send
I think if you start playing around with JPA query hints with respect to caching you can probably find a way to disable or clear the cache. For example you can evict all in JPA 2 with:



I think some of the caching options depends on your JPA provider (Hibernate, EclipseLink, etc.). More info here: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 16079 times.
Similar Threads
EJB 3 IN ACTION - EntityManager merge() and refresh() method conflict
JPA and multiple JVMs - refresh problems
transient fields and entityManager.refresh
Entitymanager.refresh doubt
CollectionOfElements
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:16:28.