Hi all!
Hibernate docs on lazy loading.
Quote:
In an application with a seperate business tier, the business logic must "prepare" all collections that will be needed by the web tier before returning. Usually, the application calls Hibernate.initialize() for each collection that will be needed in the web tier (this call must occur before the session is closed) or retrieves the collection eagerly using a query with a FETCH clause
(IMHO) I think the manual could use a little more detail here.
Im using hibernate with the spring framework (so far so good), which makes it easier to handle DAO's for each entity and associate to each DAO a sessionFactory.
Each time I call a one of the DAO methods a hibernate session is opened and closed. This means that once I load an object (say myDAO.load(id) )
this object becomes .
transient, simply because it's no longer associated to a session.
This implies that in a typical lazy collection scenario one would:
1. Load the parent object in one session (parent becomes transient) i.e:
//in Session 1
session.load(Parent.class, new Long(id));
2. Load the desired collection (this means associanting the parent to a session i.e:
// in Session 2
session.refresh(parent));
Hibernate.initialize(parent.getChildren());
So my doubts are: is there a way to associate a transient object to a session without going to the DB? Would other people recommend using Filters over this approach (in my particular case children collections are not that big).
I've had no problem so far getting lazy collections to work, but I would like to know how other people handle lazy collections in a bussines tier... Maybe I just got it all horribly wrong!! :-(