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>