以Employee和Department爲例分析如下
多對一(Employee-Department)
一對一()
多對多
多對一的(Employee-Department)
映射文件<many-to-one name="depart" column="depart_id">
depart_id 是來自另一個表的主鍵作爲外鍵,員工表中的外鍵體現員工和部門的關係
PK是主鍵
FK1爲外鍵
實例代碼如下:
Domain層的創建:
Department類的實例化:
public class Department {
private Integer id;
private String name;
privateSet<Employee> emps;
publicSet<Employee> getEmps() {
return emps;
}
public voidsetEmps(Set<Employee> emps) {
this.e配置文件的編寫代碼:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///demo</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<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>
各個包的導入:
mps = emps;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(Stringname) {
this.name = name;
}
}
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">
<class name="Department" table="department">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="emps">
<key column="depart_id"/><!-- 查找部門下所包含的員工,根據主外鍵查 -->
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee的實例化:
public class Employee {
private Integer id;
private String name;
private Department depart;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(Stringname) {
this.name = name;
}
public DepartmentgetDepart() {
return depart;
}
public voidsetDepart(Department depart) {
this.depart = depart;
}
}
Employee映射文件的創建:
<?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">
<class name="Employee" table="employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="depart"column="depart_id"/> 此處多對一的列必須要與上面set標籤中key中coulmn中的相同
</class>
</hibernate-mapping>
配置文件的編寫代碼:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///demo</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<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>
各個包的導入:
獲取Session的工具類Util的編寫:
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){
}
static{
Configuration cfg=new Configuration();
cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
此處以插入方法實現一對多的測試類的編寫:
//插入方法的實現
static Department add(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtil.getSession();
tx=session.beginTransaction();
//添加
Department dep=new Department();
dep.setName("departone");
Employee e1=new Employee();
e1.setName("Tom");
e1.setDepart(dep);//指定員工在哪個部門-----對象模型,對象建立關聯關係
session.save(dep);
session.save(e1);
tx.commit();
return dep;
}finally{
if(session!=null){
session.close();
}
}
}
主函數的調用:
public static void main(String[] args) {
// TODO Auto-generatedmethod stub
add();
}
然後以查找的形式實現的多對一測試類的編寫:
static Employee query(int empId){
Session session=null;
try{
session=HibernateUtil.getSession();
//查詢的操作
Employee e=(Employee) session.get(Employee.class,empId);
System.out.println(e.getName()+" "+e.getDepart().getName());
return e;
}finally{
if(session!=null){
session.close();
}
}
}
主函數的調用:
public static void main(String[] args){
query(1);
}