一 概念
最典型的例子是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>