我现在操作的是中间表对象
po:
public class Role_Plate extends BaseModel {private Plate plate;private UserMag_b_RightTeam role;配置hbm.xml:
<class name="com.Blank.model.Role_Plate" table="role_plate">
<id name="id" type="java.lang.String">
<column name="id" length="50" />
<generator class="uuid.hex" />
</id>
<many-to-one name="plate" class="com.Blank.model.Plate" fetch="select" >
<column name="plate_id" not-null="true" />
</many-to-one>
<many-to-one name="role" class="com.Blank.model.UserMag_b_RightTeam" fetch="select" >
<column name="role_id" not-null="true" />
</many-to-one>
而且我也都设置延迟加载了,在一的一方设置lazy="true"
plate:
<set name="rolepowers" inverse="true" lazy="true">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="com.Blank.model.Role_Plate" />
</set>
role:
<set name="rolepowers" inverse="true" lazy="true">
<key>
<column name="role_id" not-null="true" />
</key>
<one-to-many class="com.Blank.model.Role_Plate" />
</set>问题是现在我在用户action中想提取
result= this.userRoleService.getRoleFun(roleid);//返回功能与角色集合
现在这个result里面存了2个对像,一个是plate,role但是看他们对象里面的属性值都是空的,而且
ERROR - Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
怎么解决啊!!!
po:
public class Role_Plate extends BaseModel {private Plate plate;private UserMag_b_RightTeam role;配置hbm.xml:
<class name="com.Blank.model.Role_Plate" table="role_plate">
<id name="id" type="java.lang.String">
<column name="id" length="50" />
<generator class="uuid.hex" />
</id>
<many-to-one name="plate" class="com.Blank.model.Plate" fetch="select" >
<column name="plate_id" not-null="true" />
</many-to-one>
<many-to-one name="role" class="com.Blank.model.UserMag_b_RightTeam" fetch="select" >
<column name="role_id" not-null="true" />
</many-to-one>
而且我也都设置延迟加载了,在一的一方设置lazy="true"
plate:
<set name="rolepowers" inverse="true" lazy="true">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="com.Blank.model.Role_Plate" />
</set>
role:
<set name="rolepowers" inverse="true" lazy="true">
<key>
<column name="role_id" not-null="true" />
</key>
<one-to-many class="com.Blank.model.Role_Plate" />
</set>问题是现在我在用户action中想提取
result= this.userRoleService.getRoleFun(roleid);//返回功能与角色集合
现在这个result里面存了2个对像,一个是plate,role但是看他们对象里面的属性值都是空的,而且
ERROR - Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
怎么解决啊!!!
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
还有这个异常!
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
还有这个异常!是因为你使用了延迟加载,你查询时关闭了session引起的问题,要么你把lazy="false"> 不用延迟
或者可以用连接池管理session连接,就可以 不用关掉session
你应该改为lazy=false吧
延时加载关闭
一般在service层将页面上需要显示的数据准备好,如果是延迟加载的,就先在service中取出来或者用opsessioninvew模式在filter里处理session和事务
你写个过滤器来 关闭session ,在每次请求的时候关上次的session,然后从新打开一个Session 就搞定了
方法二:看你用了spring,那就直接在web.xml配置一个filter。
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
* 查询中间表,操作
*/
public List getRoleFun(String roleid) {
String sql = "from Role_Plate where role_id = '"+roleid+"' ";
return this.getHibernateTemplate().find(sql);
}
}
再 rp.getRole() 就可以得到对象了.