• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to close hibernate session?

 
Abhishek Chaudhari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The issue we have is regarding the right place to close the hibernate session.

We are using Hibernate 2.1.7.
The Person.hbm.xml file contains following code:

The corresponding java file Person.java contains

The relationship between Person & Task tables is many to many. hence there's a table called Task_Person joining these two tables.

The flow of an application is:
JSP(Submit Page) -> Action Class -> Delegate -> DAO
JSP(Result Page) <- Action Class <- Delegate <- DAO

In DAO we get a collection of 'Person' objects which is passed to delegate & further to action class.

On result page we get a collection of model classes. We need to iterate this collection & we need to iterate the set 'taskPerson' of each model object. But as 'lazy loading' is used, we need to keep the session open till the flow reaches the end of result page jsp. We are currently closing the session on this jsp.
We want to continue using lazy loading as the data is huge.
Is there any other better approach to this issue?

Thank you.

[ November 08, 2005: Message edited by: Abhishek Chaudhari ]
[ November 08, 2005: Message edited by: Abhishek Chaudhari ]
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the solution hibernate.org gives is here:

http://hibernate.org/43.html

but it is very painful (I think), although I am not sure there really is a way around it if you want to support lazy loading.

Eman
 
Magesh Lakshmi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typical way is browse through the collection and collect it in your own collection [ probably DTO collection ] , close the session and send it to the client. This way, we can eliminate long lived sessions and its NOT a good idea to expose a middle-tier session to the front end objects, owing to many reasons [ highly coupled, expensive object on HTTPSESSION ]
 
Abhishek Chaudhari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuel & Magesh,
Thanks a lot. That's really helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic