Hibernate关联映射 --- 一对多实例分析(双向关联)
一 概念
最典型的例子是Department和Employee的关系,双向的关联
二 代码分析
(1)Department表
package com.hbsi.domain;
import java.util.Set;
//部门类
public class Department {
//实现一对多
private int id;
private String nameString;
private Set<Employee> emps; //集合类型,因为有多个员工
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(int id, String nameString, Set<Employee> emps) {
super();
this.id = id;
this.nameString = nameString;
this.emps = emps;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Department [id=" + id + ", nameString=" + nameString
+ ", emps=" + emps + "]";
}
}
(2)Employee类
package com.hbsi.domain;
//员工类 一般主鍵是建在多的一方
public class Employee {
private int id;
private String name;
//通过id查询员工,通过员工 查找部门的话,只能找到部门的id,得不到部门的其他信息
// 得到的是一个 对象,可以得到员工对应的部门的详细信息
private Department depart;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(int id, String name, Department depart) {
super();
this.id = id;
this.name = name;
this.depart = 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;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", depart=" + depart
+ "]";
}
}
(3)配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<!-- 方言 针对哪个数据库Mysql -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 在程序运动的时候,增加自动创建表的属性,在程序终止 的时候销毁,但是在表格再次使用时,会重新建 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 执行的sql语句显示出来 -->
<property name="hibernate.show_sql">true</property>
<!-- 指定映射文件的位置 -->
<mapping resource="com/hbsi/domain/Department.hbm.xml" />
<mapping resource="com/hbsi/domain/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
(4)Department的映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hbsi.domain">
<!-- 缺省table 表明和类名是一样的 -->
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" />
<!-- 集合属性的体现 一对多 -->
<set name="emps">
<!-- 根据外键的值查询,而不是查询所有的记录-->
<key column="depart_id"/>
<!-- 告诉Hibernate emps是集合属性,是一对多的关联 -->
<one-to-many class="Employee" />
</set>
</class>
</hibernate-mapping>