hibernate多对一的操作解析
在hibernate的关联操作中有很多关系,其中多对一关系是最常见的。我们看这两个表。
这里有部门表和员工表。
那么我们可以这么说一个部门可以有多个员工。这就是1对多的关系。这是我们站在部门表的角度上看的。
那么我们也可以说多个员工是一个部门的,这就是多对一的关系。这就是我们站在员工表的角度上说的。
下面我做一个多对一的例子,实现两表的增加和查询操作。
1. 部门对象映射类
package com.fish.testdao;
public class Department {
private Integer id;//部门编号
private String name;//部门名称
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. 员工对象映射对象类
package com.fish.testdao;
public class Employee {
private Integer id;//员工编号
private String name;//员工姓名
private Department department;//部门类(有这个我们就可以找到部门表的信息)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
接着我们写一个部门的映射xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fish.testdao.Department" >
<id name="id" type="integer">
<generator class="increment"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
然后一写一个员工的映射xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fish.testdao.Employee" >
<id name="id" column="id"type="integer">
<generator class="increment"></generator>
</id>
<property name="name" ></property>
<many-to-one name="department" ></many-to-one>
</class>
</hibernate-mapping>
*注<many-to-onename="department" ></many-to-one>这句话就说明了我们是站在员工表的角度操作部门表的。首先name是前面映射类的属性,后面如果不加property-ref="部门表中的字段" 则说明该表默认找部门的表主键为员工表的外键。这样我们可以在数据库生成的表,department就是在员工表的外键,且字段名是department
所以我一直的理解就是多对一就是通过外键来实现的。
然后我们写一个配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD3.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://127.0.0.1:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql" >false</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/fish/testdao/Employee.hbm.xml"/>
<mapping resource="com/fish/testdao/Department.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我们写一个测试类
package com.fish.domain;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.fish.testdao.Department;
import com.fish.testdao.Employee;
public class Test {
//写一个得到session的模板
public staticSession getMySession() {
Configurationconfiguration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactoryfactory = configuration.buildSessionFactory();
Sessionsession = factory.openSession();
returnsession;
}
//对两个表数据的添加
public staticvoid add() {
Sessionsession = getMySession();
Departmentdepartment = new Department();
department.setName("财务部");
Employeeemployee = new Employee();
employee.setName("yaku");
employee.setDepartment(department);
Transactiontransaction = session.beginTransaction();
transaction.begin();
session.save(department);
session.save(employee);
transaction.commit();
session.close();
}
//通过查员工表知道他是隶属那个部门的
public staticvoid query() {
String hql ="from Employee";
Sessionsession = getMySession();
Query query =session.createQuery(hql);
List<Employee>list = query.list();
for (Employeei : list) {
System.out.println(i.getName()+"归属"+i.getDepartment().getName());
//结果是: yaku归属财务部
}
}
public staticvoid main(String[] args) throws Exception {
add();
query();
}
}