在某个表自己进行一对多的关系的时候,在hibernate配置文件中,如果这样常规配置:
<set name="childrens" inverse="true">
<key>
<column name="parentid" />
</key>
<one-to-many class="Question" />
</set>
看起来好像没什么,但运行起来,当你getChildrens后,就会出现failed to lazily initialize a collection of role这个错误。
原因可能是当你加载类似于树型结构的类时,需要一次全部加载,而不能让children在parent已经加载后再加载,解决方法是
<set name="childrens" inverse="true" lazy="false">
<key>
<column name="parentid" />
</key>
<one-to-many class="Question" />
</set>
转自http://j-wood.blog.sohu.com/33759121.html