hibernate懒加载导致多表联合查询失败
2017-06-05 19:10
417 查看
由于hibernate默认启用懒加载策略,若session关闭后则无法使用查询的内容。比如 一个部门有多个职务,一个职务有多个员工,当我们查询员工时,需要显示其职务和部门,如图
数据库为:
若service层的代码为
然后去JSP页面取数据时,部门和职务不显示。因为session已经关闭了。
JSP页面部分代码:
解决思路1: 在员工表和职务表中取消懒加载,从而查询员工时 同时 查出职务 ,查询职务时 同时 查出部门。此方法只能查出员工的职务,无法查出部门。
解决思路2: 使用spring的延迟session关闭功能,从而使jsp页面取数据时session还没关闭。成功。
解决思路3: 在service层中get一下将来要使用的数据,从而在session关闭前取得数据。成功。
数据库为:
若service层的代码为
public List<CrmStaff> findAllStaff() { return staffDao.findAll(); }
然后去JSP页面取数据时,部门和职务不显示。因为session已经关闭了。
JSP页面部分代码:
<s:iterator value="#allStaff"> <tr class="tabtd2"> <td align="center"><s:property value="staffName"/></td> <td align="center"><s:property value="gender"/></td> <td align="center"><s:date name="onDutyDate" format="yyyy-MM-dd"/></td> <td align="center"><s:property value="post.department.depName"/></td> <td align="center"><s:property value="post.postName"/></td> <td width="7%" align="center"> <a href="${pageContext.request.contextPath}/pages/staff/editStaff.jsp"><img src="${pageContext.request.contextPath}/images/button/modify.gif" class="img" /></a> </td> </tr> </s:iterator>
解决思路1: 在员工表和职务表中取消懒加载,从而查询员工时 同时 查出职务 ,查询职务时 同时 查出部门。此方法只能查出员工的职务,无法查出部门。
员工.hbm.xml配置 <!-- 多对一:多个员工 属于 【一个职务】 --> <many-to-one name="post" class="com.hantao.crm.post.domain.CrmPost" column="postId" lazy="true"></many-to-one> 职务.hbm.xml配置 <!-- 多对一:多个职务 属性 【一个部门】 --> <many-to-one name="department" class="com.hantao.crm.department.domain.CrmDepartment" column="depId" lazy="true"></many-to-one>
解决思路2: 使用spring的延迟session关闭功能,从而使jsp页面取数据时session还没关闭。成功。
<!--3 spring 过滤器,延迟session关闭--> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
解决思路3: 在service层中get一下将来要使用的数据,从而在session关闭前取得数据。成功。
public List<CrmStaff> findAllStaff() { List<CrmStaff> temp = staffDao.findAll(); for (CrmStaff crmStaff : temp) { //打印每个员工的部门名称 System.out.print(crmStaff.getPost().getDepartment().getDepName()); } return temp; }
相关文章推荐
- hibernate 多表联合查询,分页,重复记录导致的总记录数不对的解决方法。
- ibatis懒加载导致对象json化失败,ibatis和hibernate的懒加载对比
- hibernate懒加载导致对象json化失败问题解决
- 用hibernate 多表联合查询时每条记录返回的都是个对象数组
- Hibernate联合主键映射规则和数据查询原理
- warning: passing argument 2 of 'request_irq' from incompatible pointer type导致的中断申请失败和模块无法加载
- hibernate多对一lazy属性导致 s iterator session list的访问失败
- hibernate的懒加载和查询
- 通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题
- “passing argument 2 0f “request_irq” from incompatible pointer type”导致中断申请失败和模块无法加载
- hibernate的多表联合查询
- hibernate 联合查询fetch
- Hibernate多表联合查询解决办法
- VS2008 包加载失败导致VS2008打开 WebApplication项目时没有设计视图
- VS2008 包加载失败导致VS2008打开 WebApplication项目时没有设计视图
- Hibernate联合主键映射规则和数据查询原理
- 通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题
- 在 weblogic中部署struts和hibernate应用的时候提示不能加载actionservlet,部署失败。
- “passing argument 2 0f “request_irq” from incompatible pointer type”导致中断申请失败和模块无法加载
- hibernate 联合查询,返回多个表(对应着多个对象)的操作【元组】