views:

311

answers:

1

Greetings, In my domain model,

'Family' has many 'SubFamily's 'SubFamily' has many 'Locus's

and relationships are bi-directional with lazy-loading.

ie. 'Locus' can access in parent 'SubFamily' using alocus.getSubFamily();

Down below snippet I have given the DAO method to retrieve a Locus .

In my webapplication (Wicket based)

I get locus from DAO and then try to get the SubFamily as:

Locus alocus = dao.getLocusByLocusId(alocusid);
SubFamily asubfamily=alocus.getSubFamily();

But it throws the error:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150) at com.bigg.nihonbare.common.domain.SubFamily$$EnhancerByCGLIB$$a0d68bed.getSubFamilyId(

Any tips?

public Locus getLocusByLocusId(final String LocusId) {

        return (Locus) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session) {
                        Criteria criteria = session.createCriteria(Locus.class);
                        criteria.add(Expression.eq("locusId", LocusId));
                        List result =criteria.list();
                        if (result!=null && result.size() > 0) {
                            return result.get(0);
                        }
                        return null;
                    }
                });
    }
+4  A: 

It is clear that the Hibernate session is not open at the time this method is called. Are you using Spring + Hibernate or only Hibernate? If you are using spring then try with OpenSessionInView filter provided by Spring in web.xml. The filter class is org.springframework.orm.hibernate3.support.OpenSessionInViewFilter. Other wise if you are not using spring provide more details on when and where the session is being opened and closed.

Elister
I am using SPring HibernateDAOSupport , I am confused..where does actually Hibernate Session close and open without OSV filter?
umanga
The callback will use "active" hibernate session if present. If not then the session will be created, used and closed in the callback itself. If you want to use the returned object outside the callback for querying with hibernate then, you'll need to re-associate the objects with then active session. I'd suggest a much cleaner and proven OSV way to do things whenever possible.
Elister