温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
 

Hibernate级联查询(HQL)

拿雇员(Employee)与部门(Department)来举例子说明

1.1    数据库模型

1.1.1          Employee

DROP TABLE IF EXISTS employee;

CREATE TABLE employee (

 id int(11) NOT NULL auto_increment,

 empname varchar(255) default NULL,

 empage int(11) default NULL,

 context varchar(255) default NULL,

 deptid int(11) default NULL,

 PRIMARY KEY (id),

 KEY FK4722E6AEAD9148B6 (deptid)

) ENGINE=InnoDB DEFAULT CHARSET=gbk;

1.1.2          Department

DROP TABLE IF EXISTS department;

CREATE TABLE department (

 id int(11) NOT NULL auto_increment,

 deptname varchar(255) default NULL,

 PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=gbk;

1.2    Java

publicclass Employee

{

    private Integer id;

    private String empname;

    private Integer empage;

    private String context;

    private Integer deptid;

    private Department department=new Department();

      //此处省略set()get()方法

}

publicclass Department

{

    private Integer id;

    private String deptname;

    private Set<Employee> employeeSet=new HashSet<Employee>();

//此处省略set()get()方法

}

1.3    映射文件

<hibernate-mapping package="com.dl.bean">

    <class name="Employee" table="employee">

         <id name="id" >

             <generator class="native"/>

         </id>

         <property name="empname"/>

         <property name="empage"/>

         <property name="context" />

         <property name="deptid" insert="false" update="false"/>

         <many-to-one name="department" column="deptid" cascade="all" class="Department" />

    </class>

    </hibernate-mapping>

<hibernate-mapping package="com.dl.bean">

    <class name="Department" table="department">

         <id name="id" >

             <generator class="native"/>

         </id>

         <property name="deptname" />

             <set name="employeeSet">

                 <key column="deptid"></key>

                 <one-to-many class="Employee"/>

             </set>

    </class>

    </hibernate-mapping>

1.4    在写Hibernate级联查询的HQL语句时,与传统的JDBC方式有着本质的区别

1.4.1          JDBC级联写法

SELECT * FROM employee INNER JOIN department ON employee.deptid= department.id

1.4.2          HQL级联写法

select

employee.id,

employee.empname,

employee.empage,

employee.context,

employee.department.deptname

from

Employee employee

inner join employee.department (此处与前文中Employee类中的属性名一致)

order by employee.id desc

注意红色字体部分,在传统的SQL语句中inner join后面跟级联的表,但在HQLinner join后面跟能反映出级联关系的字段属性(pojo中的名字必须高度保持一致),当存在级联时,Hibernate查询结果为一对象数组,其数组的长度取决于selectfrom之间字段的个数。

1.5    注意分析下面三条语句看有什么不同

SQLQuery方式

sqlquery=session.createSQLQuery("select employee.id,employee.empname,employee.empage,employee.context,department.deptname from employee inner join department on employee.deptid=department.id");

Query方式:

session.createQuery("select employee.id,employee.empname,employee.empage,employee.context,employee.department.deptname from Employee employee inner join employee.department order by employee.id desc");

Query方式:

Query query=session.createQuery("select employee.empname, employee.department from Employee as employee order by employee.id desc");


电子文档下载
posted on 2009-11-25 16:24 雪山飞鹄 阅读(5188) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: