vinegardou 2011-10-12 08:26 采纳率: 100%
浏览 210
已采纳

大家帮我看看这个异常信息

Exception in thread "Main Thread" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.com.obj.model.po.std.SysRole.rights, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
at test.PermissionTest.main(PermissionTest.java:17)

测试:
public class PermissionTest {
public static void main(String[] args) {
ApplicationContext act=new FileSystemXmlApplicationContext("classpath:cn/com/myweb/spring/applicationContext.xml");
IUserInfoBiz userInfoBiz=(IUserInfoBiz) act.getBean("userInfoBiz");
List users=userInfoBiz.userInfoList();
System.err.println(users.size());
System.err.println(users.get(0).getRole().getRights().size());
}
}

hb.xml文件:
userinfo:









    <many-to-one name="role" class="SysRole" fetch="select" lazy="false">
        <column name="roleID" not-null="true"></column>
    </many-to-one>
 </class>

SysRole:









    <!-- 
     -->
    <bag name="rights" inverse="true" table="sys_role_right">
        <key>
            <column name="roleID" not-null="true"/>
        </key>
        <many-to-many entity-name="cn.com.obj.model.po.std.SysRight">
            <column name="rightID" not-null="true"></column>
        </many-to-many>
    </bag>

    <bag name="users" inverse="true">
        <key>
            <column name="roleID" not-null="true"></column>
        </key>
        <one-to-many class="UserInfo"/>
    </bag>
 </class>

SysRight:


















 </class>

展开全部

  • 写回答

1条回答 默认 最新

  • AngelAndAngel 2011-10-12 08:31
    关注


    这个里面加上 lazy=false

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部