• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JPA Delete Entity and Refresh Collections containing Entity

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ryan Slominski
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic