【SSH三大框架】Hibernate基础第六篇:多对一关联关系的映射、分析及加入、查询

这里举样例用的是:部门与员工的关系。

一个部门能够相应多个员工,这就是非常明显的多对一关联关系。

我们须要建立两个实体类:员工(Employee)、部门(Department)

员工类:Employee.java(在这里边我们添加了一个Department类型的属性,用来做关联)

package cn.itcast.hibernate.domain;

public class Employee {
private int id;
private String name;
private Department depart; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this.depart = depart;
}
}

部门类Department.java

package cn.itcast.hibernate.domain;

public class Department {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

然后,我们要在他们的文件夹下建立他们的映射文件:

Employee.hbm.xml(在这里我们添加了一个<many-to-one />标签用来规定映射)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="cn.itcast.hibernate.domain">
<class name="Employee" table="tb_Employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
<many-to-one name="depart" column="depart_id" />
</class> </hibernate-mapping>

Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="cn.itcast.hibernate.domain"> <class name="Department" table="tb_Department">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
</class> </hibernate-mapping>

最后我们须要建立hibernate.cfg.xml,这个文件用来建立与数据库的连接和载入各个实体类的映射文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property> <property name="hbm2ddl.auto">create</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property> <mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
<mapping resource="cn/itcast/hibernate/domain/Department.hbm.xml" />
<mapping resource="cn/itcast/hibernate/domain/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>

到了这里,全部的配置都已经配好了,就要開始測试了。

我们建立一个Many2One.java文件:

package cn.itcast.hibernate;

import org.hibernate.Session;
import org.hibernate.Transaction; import cn.itcast.hibernate.domain.Department;
import cn.itcast.hibernate.domain.Employee; public class Many2One {
public static void main(String[] arg){
add();
Employee emp = query(1);
} static Department add(){
Session s = null;
Transaction tx = null;
try{
Department depart = new Department();
depart.setName("depart name"); Employee emp = new Employee();
emp.setDepart(depart); //建立两个对象的关联关系
emp.setName("emp name"); s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(emp); //emp对象和depart对象保存的先后顺序不同也会有一定的差异
s.save(depart); //假设员工(emp)先插入,部门(depart)后插入,会生成三条数据库语句,
//由于在两者都插入之后,emp的depart_id字段还未插入,所以还要有最后一条语句插入
tx.commit();
return depart;
}finally{
if(s!=null){
s.close();
}
}
} static Employee query(int empId){
Session s = null;
Transaction tx = null;
try{
s = HibernateUtil.getSession();
tx = s.beginTransaction();
Employee emp = (Employee)s.get(Employee.class, empId); //依据ID查询
System.out.println(emp.getName()); //查询员工姓名
System.out.println(emp.getDepart().getName()); //依据员工查询属于哪个部门
tx.commit();
return emp;
}finally{
if(s!=null){
s.close();
}
}
}
}

多对一的基本操作解说完成。

由此可见,Hibernate能够直接用面向对象的语言直接操作关系型数据库。

上一篇:php 计算本月第一天 本月最后一天 下个月第一天


下一篇:Windows7下搭建Django运行环境