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.