This post is similar to what I am trying to do.
In the case where you can't handle the LazyInitializationException in the code.. it would be nice to be able to apply a ExceptionInterceptor to a Persistent class or something such that Hibernate (or custom intercepter) will seemlessly handle the exception catching and rebind the object to a new or current session. I feel there would be a lot of applications for such a design.
For instance in my post below, I mention the use of the DisplayTag taglib. Unless you get in and modify the code to recognize hibernate exceptions and do the rebinding... there is no easy way to intercept the exception at the JSP level. Currently the only way around this is to preload everything, but say for the sake of a simple example you have the following scenario:
A list of LOAN(s) is returned to the jsp and the DisplayTag iterates a users loans and displays basic details such as: id, date, amount. This is all good, and no issues yet. But if you decide to allow the user to do an csv export using the DisplayTag feature. The export includes all attributes of LOAN; those display on the table (preloaded) plus a LOAN_ACCOUNT_DETAIL reference which was not preloaded.
If this export feature was to be used by all users then it would make sense to preload the LOAN_ACCOUNT_DETAIL(s) but if it is a rarity that it is called but you still need the feature, it would be better to have it loaded ON DEMAND such is the point lazy instantiation.
So having a facility to make this happen (invisibly via way of configuration or a custom Interceptor) would be a great idea I would think. Having to modify the DisplayTag (for instance) tag to handle the Hibernate Exception would not be optimal.
Since the DisplayTag code is on the Server we should be able to somehow bind the object to a new session.
Is there already a way to do this.. similar to the Callback methods (onSave, onDelete, etc). I am really not sure how without reworking Hibernate, and whatever method throws the LazyInitializationException, this could be done.
Troy
---------- reposted -----------------
I have been reading various posts on this topic. The most significant being:
http://www.hibernate.org/124.html
I was wondering is there some pattern or design to allow lazy instantiation failover? Such as from the UI.
The very simplest would be have a configuration such that you could catch LazyInstantiationException and then take the suspect object and lock it to a newly created session.
One example I have of this is using the DisplayTag TagLib. If I don't preload the entire bean list that I pass to the UI it will throw an exception if I try to do an XML export etc. To work around this I have only found 2 solutions. The first is to preload every attribute of each bean at the DAO level when I retrieve them. This is obvously not a great solution. The second is to call a specific method in my DAO to preload specific attributes of the beans. This also is not the best since I have to anticipate what attributes will be accessed.
Another solution that I am considering is to somehow intercept the 'LazyInitializationException' and have the framework auto associate the object with a new or current session. I am not sure how to approach this exactly and would welcome comments.
Thanks
Troy McKinnon