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

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个回答


这个里面加上 lazy=false

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐