项目环境   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用了很长时间了,但是其中的细节,一直不太清楚。希望有高手指点一下。
可用积分不多,没太多的分发给大家。谢谢

解决方案 »

  1.   

    是<many-to-one>,还是<one-to-many>?
      

  2.   

    ??在部门这边是<one-to-many> 在部门的类中有 private Set<SystemEmployee> systemEmployee;    <many-to-one>在员工这边有 private SystemDepartment systemDepartment;
    网上有朋友说query。list的时候就应经级联查询了。
    咋解决呢,要是级联的话会对查询很多次的。目前还没解决。
      

  3.   

    你把lazy="true"改成lazy="false"
    或者在你的方法中在加上
    for(SystemEmployee emp: list){ //此处的list是调用listAllEmployee()方法返回的结果集
    Hibernate.initial(emp.getSystemDepartment());
    }
      

  4.   

    谢谢大家的回复我可能没说清楚我的意思 我在分页显示员工表的时候,不希望发出查询部门表的SQL,因为不需要部门的任何信息了。并不是想要部门信息而得不到。
    我已经判断出是query.list()的问题。如果用query.list()那么肯定会发出多余sql,就算你已经设置lazy="true"
    如果用HQL,例如:(下面是我的测试代码)
    String hql = "from SystemEmployee order by updateTime desc";
    return (List<SystemEmployee>)getHibernateTemplate().find(hql);
    他就只会发出1条sql。就能得到所有的员工信息。
      

  5.   

    到现在还没有想到怎么解决。分页我只知道用query来做,不知道有没有其他方式但是用query还不能lazy,sql量太大。数据量大了肯定会很慢
     
    大家谈谈,hibernate真是很难驾驭啊,等有时间了解一下iBATIS,我是直接学的hibernatejdbc都没来得及学呢!!!
      

  6.   

    贴出你输出的sql语句看看
    其实打印出连接查询语句是正确的
    延迟加载情况下,只加载相关联表的id,并不会加载其他内容
    如表
    user:
    userid
    name1表
    order :
    orderid
    name2
    userid
    其中userid是外键
    如果采用延迟加载查询user
    会查出形如下结果(值查出order表的主键)
    --------------------------
    userId  name orderId
    0       1      1如果不采用延迟加载(即立即加载)
    结果则为:
    --------------------------
    userid  name1 orderid name2
    即将表order的内容全部查出来 
      

  7.   

    配置二级缓存,不要使用,用迭代来显示数据,就不会在向数据库中发查询部门的sql语句
    而是在你打点需要的时候才会发出查询语句
      

  8.   

    还发现一个问题,就是不管多少条员工的记录,发出的SQL都是一样多的,哎!! 也许是我钻牛角尖了,但是真的不明白为啥控制不了SQL发出的数量。Hibernate: select systemempl0_.Id as Id14_, systemempl0_.department as department14_, systemempl0_.name as name14_, systemempl0_.userName as userName14_, systemempl0_.password as password14_, systemempl0_.deptName as deptName14_, systemempl0_.sex as sex14_, systemempl0_.birthday as birthday14_, systemempl0_.identityCardNo as identity9_14_, systemempl0_.identityCardAddr as identit10_14_, systemempl0_.duty as duty14_, systemempl0_.officeTel as officeTel14_, systemempl0_.homeTel as homeTel14_, systemempl0_.fax as fax14_, systemempl0_.mobile as mobile14_, systemempl0_.email as email14_, systemempl0_.qq as qq14_, systemempl0_.msn as msn14_, systemempl0_.currentAddress as current19_14_, systemempl0_.employedDate as employe20_14_, systemempl0_.leaveDate as leaveDate14_, systemempl0_.status as status14_, systemempl0_.education as education14_, systemempl0_.university as university14_, systemempl0_.graduationDate as graduat25_14_, systemempl0_.notes as notes14_, systemempl0_.creater as creater14_, systemempl0_.createTime as createTime14_, systemempl0_.updater as updater14_, systemempl0_.updateTime as updateTime14_ from crm.system_employee systemempl0_ order by systemempl0_.updateTime desc limit ?
    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=?