Java笔记之hibernate(七):一对多

0.说在前面

  基于Hibernate(六):多对一项目

1.修改Department类,添加Employee的Set集合属性,并去掉toString()方法或者去除toString方法中关于Employee的Set集合的部分---为了避免双向一对多的循环调用使堆栈溢出

package com.hibernate.demo.bean;

import java.util.Set;

public class Department {
    private Integer deptId;
    private String deptName;
    private Set<Employee> employees;
    public Department() {
        super();
        
    }
    public Department(Integer deptId, String deptName, Set<Employee> employees) {
        super();
        this.deptId = deptId;
        this.deptName = deptName;
        this.employees = employees;
    }
    public Integer getDeptId() {
        return deptId;
    }
    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public Set<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }
}

2.修改Department.hbm.xml文件,添加一对多的配置

<?xml version="1.0" encoding="UTF-8"?>
<!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.hibernate.demo.bean">
    <!-- 映射Department类与表t_department -->
    <class name="Department" table="t_department">
        <id name="deptId" column="dept_id">
            <!-- 声明主键生成策略为自增 -->
            <generator class="native"></generator>
        </id>
        <property name="deptName" column="dept_name"></property>
        <!-- 用于设置一对多的关系 -->
        <set name="employees">
            <!-- 声明多的一方对应的表中的外键列名,not-null设置可以为空 -->
            <key column="dept_id" not-null="false"></key>
            <!-- 声明一对多的关系,class设置多的一方对应的类名 -->
            <one-to-many class="Employee"/>
        </set>
    </class>
</hibernate-mapping>

3.修改Employee类,去除toString()方法或者去除toString方法中关于Department对象的部分,理由同1

package com.hibernate.demo.bean;

public class Employee {
    private Integer empId;
    private String empName;
    private Department department;
    
    public Employee() {
        
    }

    public Employee(Integer empId, String empName, Department department) {
        super();
        this.empId = empId;
        this.empName = empName;
        this.department = department;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

}

4.新建OneToManyTest类

package com.hibernate.demo.test;

import java.util.Set;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.hibernate.demo.bean.Department;
import com.hibernate.demo.bean.Employee;

public class OneToManyTest {

    public static void main(String[] args) {
        //加载配置文件,创建会话工厂对象
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //创建会话工厂对象
        Session session = sessionFactory.openSession();
        //根据id获取Department对象
        Department department = (Department) session.get(Department.class, 5);
        System.out.println("Department:"+department.getDeptId()+"---"+department.getDeptName());
        //获取多的一方的集合
        Set<Employee> employees = department.getEmployees();
        //遍历集合
        for (Employee employee : employees) {
            System.out.println("Employee:"+employee.getEmpId()+"---"+employee.getEmpName());
        }
        
        //关闭会话对象
        session.close();
        //关闭会话工厂对象
        sessionFactory.close();
    }

}

5.运行OneToManyTest类

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select department0_.dept_id as dept1_1_0_, department0_.dept_name as dept2_1_0_ from t_department department0_ where department0_.dept_id=?
Department:5---商务部
Hibernate: select employees0_.dept_id as dept3_1_1_, employees0_.emp_id as emp1_1_, employees0_.emp_id as emp1_0_0_, employees0_.emp_name as emp2_0_0_, employees0_.dept_id as dept3_0_0_ from t_employee employees0_ where employees0_.dept_id=?
Employee:18---emp
Employee:17---张三

猜你喜欢

转载自www.cnblogs.com/antusheng/p/12759053.html