Saturday, August 27, 2005

The classic LazyInitializationException


public class CategoryDAOImpl
extends HibernateDaoSupport
implements ICategoryDAO
{
//~ Methods -------------------------------------


public List loadAll( ) {
return getHibernateTemplate( ).loadAll( Category.class );
}

public List loadAllWithChildren(){
return getHibernateTemplate().find("from Category category left join fetch category.children");
}
}


I've seen a million and one post about how to load object associations at run time, without having hibernate through a LazyInitializationException.
There seems to be two ways on doing this, in a DAO using the left join fetch clause, or if you are using Spring use the OpenSessionInViewFilter, this will create a hibernate session per user thread in a web application.

1 comment:

/^\ Pharaoh /^\ said...

I found a cleaner way to do this if your using spring AOP.

check our Karum log.

http://www.jroller.com/comments/kbaum/Weblog/orm_lazy_initialization_with_dao