Wow!! I did not realize that managing sessions in MVC was going to be such a complicated issue. I have read the following:
http://www.hibernate.org/43.html -- Open Session in View Pattern
and also
http://www.hibernate.org/43.html -- Forum Discussion about this
So my question is this. I have a layered application where hibernate generated objects are my data layer. Then I have a layer of business objects, then of course my view (which happens to be a struts application).
I am in the process of converting my app to use hibernate, mainly due to performance issues, and I want to make sure that I implement it in a way that makes the most sense. I am struggling with the question of how to handle my sessions for the view and bus portions of the application.
Right now, in my bus logic for all of my transactions, inserts, deletes, selects, etc... I open a session do my stuff then close the session. This works all fine and dandy of course until I get to the presentation side of things and need to display an association of some sort. Like say a one to many.
I saw the Open Session in View pattern and I guess that will work to get the associated objects while rendering the view; however, it looks a little complicated. So my questions are:
1) Is there an easier way to do this (e.g. load objects during the view)?
2) Is there any way to say getAssociatedCollection() in the bus layer and just pass the object up to the view already initialized without having to open another session?
3) Is opening two sessions (one for the bus layer and one for getting the associated objects during the view) a bad design? If I use the Open Session in View Pattern, I will be essentially opening two sessions. One for the select down in the bus logic and one when presenting the view.
Mainly I am just looking for ideas and also to see if anyone else has solved this in an elegant way?
Thanks,
D