• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible/practical to use lazy loading with servlets and JSPs?

 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a situation where I'm displaying data from an object that has been lazily loaded via Hibernate. The object contains a number of collections that are shown as unexpanded links on a JSP. I'm trying to use lazy loading to avoid reading in the complete data tree, i.e. I want the detailed information contained in the collection to be fetched from the DB only if the associated link is selected.

What I want to happen is for the magic of Hibernate to recognize that the collection is part of a lazy loaded object, and then go fetch the underlying collection data.

What happens is that it does know that the collection needs to be fetched, but I get the following exception:


All my research thus far leads me to understand that lazy loading only works when you maintain a persistent Hibernate Session for all accesses to the object. Is my understanding correct? If this is the case, I would practically need to keep a single Hibernate Session throughout a servlet session. If so, can anyone point me in the direction of more words on this topic that might help me develop a strategy for maintaining a persistent Hibernate Session through multiple posts from a servlet?
[ December 22, 2005: Message edited by: Bridget Kennedy ]
 
pranith rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bridget Kennedy:
I've got a situation where I'm displaying data from an object that has been lazily loaded via Hibernate. The object contains a number of collections that are shown as unexpanded links on a JSP. I'm trying to use lazy loading to avoid reading in the complete data tree, i.e. I want the detailed information contained in the collection to be fetched from the DB only if the associated link is selected.

What I want to happen is for the magic of Hibernate to recognize that the collection is part of a lazy loaded object, and then go fetch the underlying collection data.

What happens is that it does know that the collection needs to be fetched, but I get the following exception:


All my research thus far leads me to understand that lazy loading only works when you maintain a persistent Hibernate Session for all accesses to the object. Is my understanding correct? If this is the case, I would practically need to keep a single Hibernate Session throughout a servlet session. If so, can anyone point me in the direction of more words on this topic that might help me develop a strategy for maintaining a persistent Hibernate Session through multiple posts from a servlet?


Use a filter to open the session and close the session for the servlet or you can use DTO assembler pattern to create a DTO for the view layer so that lazy initialization does not effect. I use the second option
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I'm trying to understand how the DTO assembler pattern what would be used with Hibernate lazy loading.

use DTO assembler pattern to create a DTO for the view layer so that lazy initialization does not effect.


Do you write your code to specifically fetch the underlying collection when it is referenced vs letting Hibernate fetch it for you?
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing this w/o using Spring or other frameworks...it's not very hard at all. I've got a domain model layer, a DAO layer where each class has a constructor that starts the transaction, and in my presentation (servlet) layer there is a single Filter that closes the transaction & connection.

This was really frustrating for me at first but Hibernate in Action has a chapter about design that shows you how to do what I've described above.
[ January 02, 2006: Message edited by: Vinnie Jenks ]
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Vinnie.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pranith kadaru:
[CODE]you can use DTO assembler pattern to create a DTO for the view layer so that lazy initialization does not effect.


I'm reading Hibernate in Action, and they refer to the "DTO (anti-) pattern" (p. 119) and later state (p. 312):

The DTO pattern exhibits two of the code smells described in Fowler[199]: the
shotgun change smell, where a small change to some system requirement requires
changes to multiple classes; and the parallel class hierarchies smell, where two
different class hierarchies contain similar classes in a one-to-one correspondence.


So do Hibenaters hate the smell of DTOs?
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Their arguments against DTOs don't mention Hibernate. They're generic objections, regardless of whether you use Hibernate or not.
 
dave lund
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to be going to a lot of trouble...

You can re-attach an object to a different hibernate session using the Session.lock(Obj,LockMode.NONE).

Using the NONE is prefect for lazy loading because it will leave the object unlocked until a lazy initialised property is request, it will then grab a read-only lock on the database.... and no long running session are needed! Theres probably a pattern for this somewhere out there
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic