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

0.说在前面

  基于Hibernate(五):标准SQL项目

1.修改Employee类,添加Department类型的属性department,它们之间的关系为多对一的关系

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;
    }

    @Override
    public String toString() {
        return "Employee [empId=" + empId + ", empName=" + empName + ", department=" + department + "]";
    }
}

2.修改Employee.hbm.xml文件,添加many-to-one的配置

<?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">
    <!-- 映射Employee类与t_employee表 -->
    <class name="Employee" table="t_employee">
        <id name="empId" column="emp_id">
            <!-- 声明主键生成策略为自增 -->
            <generator class="native"></generator>
        </id>
        <property name="empName" column="emp_name"></property>
        <!-- 多对一,name指定多的一方中一的一方的属性名称,class指定一的一方类名,column指定多的一方表中指向一的一方表的外键字段 -->
        <many-to-one name="department" class="Department" column="dept_id"></many-to-one>
    </class>
</hibernate-mapping>

3.新建Department类

package com.hibernate.demo.bean;

public class Department {
    private Integer deptId;
    private String deptName;
    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;
    }
    @Override
    public String toString() {
        return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
    }
    
}

4.新建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>
    </class>
</hibernate-mapping>

5.修改hibernate.cfg.xml文件,添加Department.hbm.xml映射配置

<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/keeper?characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <!-- MySQL方言配置 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/hibernate/demo/bean/Employee.hbm.xml"/>
        <mapping resource="com/hibernate/demo/bean/Department.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

6.新建ManyToOneTest类

package com.hibernate.demo.test;

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

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

public class ManyToOneTest {

    public static void main(String[] args) {
        //加载配置文件,创建会话工厂对象
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //创建会话对象
        Session session = sessionFactory.openSession();
        //开始事务
        Transaction transaction = session.beginTransaction();
        //新建Department对象
        Department department=new Department();
        department.setDeptName("商务部");
        session.save(department);
        //新建Employee对象
        Employee employee1=new Employee();
        employee1.setEmpName("张三");
        employee1.setDepartment(department);
        session.save(employee1);
        
        Employee employee2=new Employee();
        employee2.setEmpName("emp");
        employee2.setDepartment(department);
        session.save(employee2);
        //提交事务
        transaction.commit();
        //关闭会话对象
        session.close();
        //关闭会话工厂对象
        sessionFactory.close();
    }

}

7.运行ManyToOneTest类并查看数据库

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: insert into t_department (dept_name) values (?)
Hibernate: insert into t_employee (emp_name, dept_id) values (?, ?)
Hibernate: insert into t_employee (emp_name, dept_id) values (?, ?)

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

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

 

上一篇:ROLLUP、CUBE、GROUP BY的使用区别


下一篇:mybatis+servlet