hibernate常見關係的操作

一對多和多對一
爲了表示這種關係,我們需要先建立一個部門表,然後再建立一個員工表。

這裏寫圖片描述

public class Employee {
    private int employeeId;
    private String employeeName;
    public Department department;

    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 Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = 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="model">
    <class table="Employee" name="Employee">
        <id name="EmployeeId" column="EmployeeId" type="int">
            <generator class="increment"></generator>
        </id>
        <property name="employeeName" column="employeeName" type="string"></property>
        <many-to-one name="department" column="departmentId" class="model.Department" not-null="true">
        </many-to-one>
    </class>

</hibernate-mapping>

在寫實體對象的時候需要注意,這時候寫的就不是departmentId了,類型也變成來Department,當然還要添加對應的set和get方法。
同時注意的還有配置文件,最後寫的是many to one標籤而不是property,裏面也要添加class屬性。

接下來是部門department

public class Department {
    private int departmentId;
    private String departmentName;
    public Set<Employee> employees=new HashSet<Employee>();

    public int getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public Set<Employee> getEmployees() {
        return employees;
    }

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

在部門這裏要注意以下,實際的department中並沒有Employee,但這裏爲了表示一對多的這種關係,我們需要加一個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="model">
    <class table="Department" name="Department">
        <id name="departmentId" column="departmentId" type="int">
            <generator class="increment"></generator>
        </id>
        <property name="departmentName" column="departmentName" type="string"></property>

        <set name="employees" table="Employee" >
           <key column="departmentId"></key>
            <one-to-many class="Employee"></one-to-many>
        </set>
    </class>

</hibernate-mapping>

這裏的配置文件也需要注意,添加上一個set集合,對應的表是Employee,映射的是表中的departmentId字段,並且是一對多的關係.

        Configuration configuration=new Configuration();
        configuration.configure("hibernate-cfg.xml");
        SessionFactory sessionFactory=configuration.buildSessionFactory();
        Session session=sessionFactory.openSession();
        Transaction transaction=session.beginTransaction();
        Department department=new Department();
        department.setDepartmentName("java開發");
        session.save(department);
        Employee employee= new Employee();
        employee.setEmployeeName("Hello");
        /**********************************/
        employee.setDepartment(department);
        department.employees.add(employee);
        /*********************************/
        session.save(employee);

        transaction.commit();

注意我註釋中間的兩條代碼,爲了表示Employee和Department的這種對應關係,我們需要讓他們兩進行相互添加,如果學過數據庫的人覺的這兩句代碼看起來很彆扭,那是正常的,因爲這涉及到了hibernate的緩存的知識。
這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章