Java笔记之hibernate(八):多对多

0.说在前面

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

1.修改Employee类,添加Project类的Set集合属性projects

package com.hibernate.demo.bean;

import java.util.Set;

public class Employee {
    private Integer empId;
    private String empName;
    private Department department;
    private Set<Project> projects;
    
    public Employee() {
        
    }

    public Employee(Integer empId, String empName, Department department, Set<Project> projects) {
        super();
        this.empId = empId;
        this.empName = empName;
        this.department = department;
        this.projects = projects;
    }

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

    public Set<Project> getProjects() {
        return projects;
    }

    public void setProjects(Set<Project> projects) {
        this.projects = projects;
    }

    
}

2.修改Employee.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">
    <!-- 映射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>
        <!-- 用于配置多对多的关系,table用于指定中间表的表名 -->
        <set name="projects" table="t_peitem">
            <!-- 中间表用于指向Employee对应表的外键列名-->
            <key column="emp_id"></key>
            <!-- 声明多对多的关系,column用于指定中间表指向Project类对应表的外键列名 ,class指定多的另一方的类名-->
            <many-to-many column="pro_id" class="Project"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

3.新建Project类

package com.hibernate.demo.bean;

import java.util.Set;

public class Project {
    private Integer proId;
    private String proName;
    private Set<Employee> employees;
    
    public Project() {
        
    }
    public Project(Integer proId, String proName, Set<Employee> employees) {
        super();
        this.proId = proId;
        this.proName = proName;
        this.employees = employees;
    }
    public Integer getProId() {
        return proId;
    }
    public void setProId(Integer proId) {
        this.proId = proId;
    }
    public String getProName() {
        return proName;
    }
    public void setProName(String proName) {
        this.proName = proName;
    }
    public Set<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }
    
    
}

4.新建Project.hbm.xml文件

<!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">
    <!-- 映射Project类与表t_project -->
    <class name="Project" table="t_project">
        <id name="proId" column="pro_id">
            <!-- 指定主键生成策略为自增 -->
            <generator class="native"></generator>
        </id>
        <property name="proName" column="pro_name"></property>
        <!-- 用于配置多对多的关系,table用于指定中间表的表名 -->
        <set name="employees" table="t_peitem">
            <!-- 中间表指向 Project类对应表的外键列名-->
            <key column="pro_id"></key>
            <!-- 声明多对多的关系,column指定中间表指向Employee类对应表的外键列名, class指向多的另一方的类名-->
            <many-to-many column="emp_id" class="Employee"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

5.修改hibernate.cfg.xml文件,添加Project.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"/>
        <mapping resource="com/hibernate/demo/bean/Project.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

6.新建ManyTOManyTest类

package com.hibernate.demo.test;

import java.util.Set;

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

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

public class ManyToManyTest {

    public static void main(String[] args) {
        //加载配置文件,创建会话工厂对象
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //创建会话对象
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction transaction = session.beginTransaction();
        //根据id获取Employee对象
        Employee employee1 = (Employee) session.get(Employee.class, 17);
        Employee employee2=(Employee) session.get(Employee.class, 18);
        //新建项目
        Project project1=new Project();
        project1.setProName("项目五");
        session.save(project1);
        Project project2=new Project();
        project2.setProName("项目六");
        session.save(project2);
        
        //设置项目
        Set<Project> projects = employee1.getProjects();
        projects.add(project1);
        projects.add(project2);
        session.update(employee1);
        
        Set<Project> projects2 = employee2.getProjects();
        projects2.add(project1);
        projects2.add(project2);
        session.update(employee2);
        
        //提交事务
        transaction.commit();
        //关闭会话
        session.close();
        //关闭会话工厂
        sessionFactory.close();
    }

}

7.运行ManyTOManyTest类,查看数据库

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 employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=?
Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=?
Hibernate: insert into t_project (pro_name) values (?)
Hibernate: insert into t_project (pro_name) values (?)
Hibernate: select projects0_.emp_id as emp1_0_1_, projects0_.pro_id as pro2_1_, project1_.pro_id as pro1_3_0_, project1_.pro_name as pro2_3_0_ from t_peitem projects0_ inner join t_project project1_ on projects0_.pro_id=project1_.pro_id where projects0_.emp_id=?
Hibernate: select projects0_.emp_id as emp1_0_1_, projects0_.pro_id as pro2_1_, project1_.pro_id as pro1_3_0_, project1_.pro_name as pro2_3_0_ from t_peitem projects0_ inner join t_project project1_ on projects0_.pro_id=project1_.pro_id where projects0_.emp_id=?
Hibernate: insert into t_peitem (emp_id, pro_id) values (?, ?)
Hibernate: insert into t_peitem (emp_id, pro_id) values (?, ?)
Hibernate: insert into t_peitem (emp_id, pro_id) values (?, ?)
Hibernate: insert into t_peitem (emp_id, pro_id) values (?, ?)

t_project表

t_peitem表按照emp_id升序排列

t_peitem表按照pro_id升序排列

posted @ 2020-04-23 11:25  安徒生敲代码  阅读(143)  评论(0编辑  收藏  举报