failed to lazily initialize a collection of role

来源:互联网 发布:汉倭奴国王金印 知乎 编辑:程序博客网 时间:2024/05/16 11:58

failed to lazily initialize a collection of role

分享
分类: 技术2007-02-12 20:11

在某个表自己进行一对多的关系的时候,在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>
原创粉丝点击