开发者

Spring HibernateDaoSupport : lazy-loading problem?

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=alocu开发者_如何学运维s.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;
                    }
                });
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜