i have entity parent , relation @onetomany
entity child. collection children set lazy loading.
@entity class parent{ @id string parentid; @onetomany(mappedby="parent",fetch=fetchtype.lazy) list<child> children; } @entity class child{ @id string childid; @manytoone @joincolumn(name="parentid") parent parent; } list<parent> loadparents() { qparent qparent = qparent.parent; list<parent> parentlist = query.select(qparent).from(qparent).fetch(); return parentlist; } @transactional void test(){ list<parent> parentlist = loadparents(); for(child child:parentlist.getchildren()){ child.getchildid(); } }
i famous
org.hibernate.lazyinitializationexception: failed lazily initialize collection of role ... not initialize proxy - no session
exception in test() method on line access children list.
i don't want change fetch type annotation in entity.
how access child entities?
i found culprit. transaction management disabled.
the @transactional annotation missing test method.
to enable transaction management, put in application-context.xml:
<tx:annotation-driven />
there nothing wrong code, configuration incomplete. eagerly load nested collections need embracing transaction.
turning on debug logging org.springframework.orm , org.hibernate helped me identify source of issue.
similar question , answer: lazyinitializationexception in jpa , hibernate
Comments
Post a Comment