Department部門和Employee員工是一對多的關系,再多的一端會有一個外鍵來維護關系,即多的一端是關系的維護端,少的一端為關系的被維護端。
Hibernate 中文手冊 PDF http://www.linuxidc.com/Linux/2013-10/91208.htm
部門實體Department.java:
/**
* 部門和員工的關系為一對多,多的一端為關系的維護端,
* 關系的維護端來負責外鍵的更新,少的一端無權更新外鍵字段。
* @author Liao
*/
public class Department {
/* 部門Id */
private Integer id;
/* 部門名稱 */
private String name;
/* 部門和員工的關系(一對多) */
private Set<Employee> employees;
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 Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
員工實體Employee.java:
public class Employee {
/* 員工ID */
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;
}
}
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">
<!-- package表示實體類的包名 -->
<hibernate-mapping package="com.lixue.bean">
<!-- class結點的name表示實體的類名,table表示實體映射到數據庫中table的名稱 -->
<class name="Department" table="t_department">
<id name="id" column="did">
<!-- 自增長 -->
<generator class="native"/>
</id>
<property name="name" column="dname"/>
<!-- 部門和員工的關系(一對多) -->
<set name="employees" cascade="save-update">
<key column="dept_id"/><!-- 表示在多的一端加一個字段 -->
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.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">
<!-- package表示實體類的包名 -->
<hibernate-mapping package="com.lixue.bean">
<!-- class結點的name表示實體的類名,table表示實體映射到數據庫中table的名稱 -->
<class name="Employee" table="t_employee">
<id name="id" column="eid">
<!-- 自增長 -->
<generator class="native"/>
</id>
<property name="name" column="ename"/>
<!-- 設置Employee和Department的關系column表示和Department.hbm.xml文件的外鍵映射要一致 -->
<many-to-one name="department" column="dept_id"/>
</class>
</hibernate-mapping>
注:Employee為多的一端,作為關系的維護端,通過一個外鍵即dept_id來維護。
測試類:
public class One2ManyTest {
public static void main(String[] args) {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
/*創建部門並設置屬性*/
Department department = new Department();
department.setName("開發部");
session.save(department);
/*創建員工並設置屬性*/
Employee employee = new Employee();
employee.setName("習近平");
employee.setDepartment(department);
session.save(employee);
transaction.commit();
}
}
注:我這裡只是隨便測試了一下,並未關閉Session和Transaction。
Hibernate 的詳細介紹:請點這裡
Hibernate 的下載地址:請點這裡