项目环境 struts2(2.1.6) spring2.5.5 hibernate3.3.1很简单2张表,部门表和员工表。部门表的映射文件如下:
………………
………………
<set name="SystemEmployee" inverse="true" lazy="true" table="system_employee">
<key>
<column name="department" length="36" />
</key>
<one-to-many class="SystemEmployee" />
</set>
<many-to-one name="parent" class="SystemDepartment" lazy="proxy">
<column name="pid" />
</many-to-one>
…………
员工表如下: …………
<many-to-one name="systemDepartment" class="SystemDepartment" lazy="proxy">
<column name="department" length="36" />
</many-to-one>
<property name="name" type="string" not-null="true">
<column name="name" length="50" />
</property>
……………………
……………………DAO中的分页方法
@Override
public List<SystemEmployee> listAllEmployee(PageBean pageBean) {
Query query = getSession().createQuery("from SystemEmployee order by updateTime desc");
query.setFirstResult((pageBean.getCurrentPage()-1)*pageBean.getPageSize());
query.setMaxResults(pageBean.getPageSize());
return (List<SystemEmployee>)query.list();
}为了查询更快一点,我已经又在员工表里加入部门名称这个字段了,希望能延迟加载,但是不管怎么设置lazy属性(俩个表都设置了),但是发出的sql还是一样的。都进行了左外连接查询部门表。网上查了很多资料,按照做了但是都没有作用。我不想用open session in view
hibernate用了很长时间了,但是其中的细节,一直不太清楚。希望有高手指点一下。
可用积分不多,没太多的分发给大家。谢谢
………………
………………
<set name="SystemEmployee" inverse="true" lazy="true" table="system_employee">
<key>
<column name="department" length="36" />
</key>
<one-to-many class="SystemEmployee" />
</set>
<many-to-one name="parent" class="SystemDepartment" lazy="proxy">
<column name="pid" />
</many-to-one>
…………
员工表如下: …………
<many-to-one name="systemDepartment" class="SystemDepartment" lazy="proxy">
<column name="department" length="36" />
</many-to-one>
<property name="name" type="string" not-null="true">
<column name="name" length="50" />
</property>
……………………
……………………DAO中的分页方法
@Override
public List<SystemEmployee> listAllEmployee(PageBean pageBean) {
Query query = getSession().createQuery("from SystemEmployee order by updateTime desc");
query.setFirstResult((pageBean.getCurrentPage()-1)*pageBean.getPageSize());
query.setMaxResults(pageBean.getPageSize());
return (List<SystemEmployee>)query.list();
}为了查询更快一点,我已经又在员工表里加入部门名称这个字段了,希望能延迟加载,但是不管怎么设置lazy属性(俩个表都设置了),但是发出的sql还是一样的。都进行了左外连接查询部门表。网上查了很多资料,按照做了但是都没有作用。我不想用open session in view
hibernate用了很长时间了,但是其中的细节,一直不太清楚。希望有高手指点一下。
可用积分不多,没太多的分发给大家。谢谢
网上有朋友说query。list的时候就应经级联查询了。
咋解决呢,要是级联的话会对查询很多次的。目前还没解决。
或者在你的方法中在加上
for(SystemEmployee emp: list){ //此处的list是调用listAllEmployee()方法返回的结果集
Hibernate.initial(emp.getSystemDepartment());
}
我已经判断出是query.list()的问题。如果用query.list()那么肯定会发出多余sql,就算你已经设置lazy="true"
如果用HQL,例如:(下面是我的测试代码)
String hql = "from SystemEmployee order by updateTime desc";
return (List<SystemEmployee>)getHibernateTemplate().find(hql);
他就只会发出1条sql。就能得到所有的员工信息。
大家谈谈,hibernate真是很难驾驭啊,等有时间了解一下iBATIS,我是直接学的hibernatejdbc都没来得及学呢!!!
其实打印出连接查询语句是正确的
延迟加载情况下,只加载相关联表的id,并不会加载其他内容
如表
user:
userid
name1表
order :
orderid
name2
userid
其中userid是外键
如果采用延迟加载查询user
会查出形如下结果(值查出order表的主键)
--------------------------
userId name orderId
0 1 1如果不采用延迟加载(即立即加载)
结果则为:
--------------------------
userid name1 orderid name2
即将表order的内容全部查出来
而是在你打点需要的时候才会发出查询语句
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid is null
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?
Hibernate: select systemdepa0_.Id as Id13_, systemdepa0_.deptName as deptName13_, systemdepa0_.teamLeader as teamLeader13_, systemdepa0_.deptDescription as deptDesc4_13_, systemdepa0_.notes as notes13_, systemdepa0_.pid as pid13_ from crm.system_department systemdepa0_ where systemdepa0_.pid=?