Hi,
I have an application with a fairly complex domain model. For the most part, persistence is straightforward but I have come across a few cases where I need to evict objects from the Persistence Context. An example of this might be where a client app is supplying a remove request to a business service:
Code:
@Transactional
public void removeFoo (final Integer fooId) {
// We need to know what the "x" property of the object is before we delete it
// This will have brought a whole load of associated objects into the Persistence Context with it
Foo fooToRemove = this.fooPersistenceService.get (fooId);
// Now take some action based on the property
if (fooToRemove.getX() == 42) {
// Do Something
}
// Need to get rid of Foo to make sure it doesn't get "re-saved by cascade"
this.fooPersistenceService.evict(fooToRemove);
// Now we can remove the object
this.fooPersistenceService.remove(fooId);
}
This is usually not a problem. However, I have noticed that as the domain relationships become more complex and there are more bi-directional relationships, evicting an object completely becomes more and more difficult as I have to ensure that the evict is cascaded properly. I understand that this is for good reason.
It would be far simpler for me to use Session.clear(). There are a handful of wierd corner cases in the app where this would make life a lot easier.
My apologies for not providing actual code but, unfortunately, this is not possible. However, this is more of a design question so hopefully the lack of real code is not a problem.
So, my questions are:
1) Is there any overhead associated with calling clear() instead of evict()?
2) Would the experts consider me replacing evict() with clear() to be a monumental hack?
Thanks in advance,
Si