1. 程式人生 > >hibernate之初學一對多和多對一配置及使用

hibernate之初學一對多和多對一配置及使用

外鍵 ble dtd nat util gin doc 一對多 dialect

按查詢及存取速率來說的一對多用的相對多對一少一些,這裏只寫主要配置文件的代碼

首先是hibernate的配置文件

<!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:///webproject</property> <property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property
> <property name="hibernate.hbm2ddl.auto">create</property> <mapping resource="com/shaoxin/entity/Dept.hbm.xml"/> <mapping resource="com/shaoxin/entity/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>

一對多的配置文件代碼,很明顯使用到了set集合

<?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.shaoxin.entity">
        <class name="Dept" table="dept">
            <id name="deptid" column="deptid">
                <generator class="native"></generator>
            </id>
            <property name="deptName" column="deptname"></property><!--
            一對多設置外鍵方式,class代表外鍵類型
            --><set name="setEmployees" table="employee">
                <key column="dept_id"></key>
                <one-to-many class="Employee"/>
            </set>
        </class>
    </hibernate-mapping>

對應的實體類:

package com.shaoxin.entity;

import java.util.HashSet;
import java.util.Set;

public class Dept {
    private int deptid;
    private String deptName;
    private Set<Employee> setEmployees = new HashSet<Employee>();

    public int getDeptid() {
        return deptid;
    }

    public void setDeptid(int deptid) {
        this.deptid = deptid;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Set<Employee> getSetEmployees() {
        return setEmployees;
    }

    public void setSetEmployees(Set<Employee> setEmployees) {
        this.setEmployees = setEmployees;
    }

}

多對一的配置文件代碼,很明顯是一個對象類型

<?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.shaoxin.entity">
        <class name="Employee"  table="employee">
            <id name="employeeid" column="employeeid">
                <generator class="native"></generator>
            </id>
            <property name="employeeName" column="employeename"/><!--
            多對一創建外鍵方式:屬性分別為:外鍵對應的表,外鍵字段,外鍵類型
            --><many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
        </class>
    </hibernate-mapping>

對應的實體類

package com.shaoxin.entity;

public class Employee {
    private int employeeid;
    private String employeeName;
    private Dept dept;

    public int getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(int employeeid) {
        this.employeeid = employeeid;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

}

測試這兩個存儲方式

package com.shaoxin.entity;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

public class TestMany2Many {
    static SessionFactory sf;
    static {
        sf = new Configuration().configure().buildSessionFactory();
    }

    @Test
    public void testOne2Many() throws Exception {
        Session openSession = sf.openSession();
        Transaction beginTransaction = openSession.beginTransaction();
        Employee empzs = new Employee();
        Employee empls = new Employee();
        empls.setEmployeeName("李四");
        empzs.setEmployeeName("張三");
        Dept dept = new Dept();
        dept.getSetEmployees().add(empls);
        dept.getSetEmployees().add(empzs);
        dept.setDeptName("應用開發");
        openSession.save(empls);
        openSession.save(empzs);
        openSession.save(dept);
        beginTransaction.commit();
        openSession.close();
    }

    @Test
    public void testMany2One() throws Exception {
        Session openSession = sf.openSession();
        Transaction beginTransaction = openSession.beginTransaction();
        Employee empzs = new Employee();
        Employee empls = new Employee();
        Dept dept = new Dept();
        dept.setDeptName("應用開發");
        empls.setEmployeeName("李四");
        empzs.setEmployeeName("張三");
        empls.setDept(dept);
        empzs.setDept(dept);
        openSession.save(dept);
        openSession.save(empls);
        openSession.save(empzs);
        beginTransaction.commit();
        openSession.close();
    }
}

hibernate之初學一對多和多對一配置及使用