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的缓存的知识。
这里写图片描述

这里写图片描述

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