wood burning stoves*
The moose likes Object Relational Mapping and the fly likes JPA Delete Entity and Refresh Collections containing Entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA Delete Entity and Refresh Collections containing Entity" Watch "JPA Delete Entity and Refresh Collections containing Entity" New topic
Author

JPA Delete Entity and Refresh Collections containing Entity

Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
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.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
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!
Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPA Delete Entity and Refresh Collections containing Entity