首页 > 代码库 > hibernate 一对多(级联关系)

hibernate 一对多(级联关系)

hibernate 核心配置文件

<?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>
        <!-- DataSource配置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        
        <!-- 指定连接数据库所使用的SQL方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 控制台是否输出SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 式化Hibernate的SQL输出语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 指定程序是否在数据库自动创建表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 指定映射文件的路径 -->
        <mapping resource="zr/com/pojo/TeamClass.hbm.xml"/>
        <mapping resource="zr/com/pojo/Student.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

实体类的映射关系

<?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="zr.com.pojo">
    <!-- 配置映射关系 -->
    <class name="TeamClass" table="team_class">
        <!-- 配置主键和对应主键的属性 -->
        <id name="classID" column="class_id">
            <generator class="native"/>
        </id>
        <!-- 配置属性和相对应数据的列 -->
        <property name="lfname" column="class_name"/>
        <!-- 集合 
            cascade="all" 级联关系
            inverse="true" 放弃管理外键
        -->
        <set name="students" table="student" cascade="all" inverse="true">
            <!-- 外键 -->
            <key column="class_id"/>
            <!-- 一对多 -->
            <one-to-many class="zr.com.pojo.Student"/>
        </set>
    </class>

</hibernate-mapping>     
<?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>
     
     <!-- 映射关系 -->
     <class name="zr.com.pojo.Student" table="student">
         <id name="id" column="stu_id">
             <!-- 生成主键id的方式 -->
            <generator class="native"/>
         </id>
         <property name="name" column="stu_name"/>
         <!-- <property name="classId" column="class_id"/> -->
         
         <many-to-one name="teamClass" class="zr.com.pojo.TeamClass" column="class_id" cascade="all"/>
         
     </class>

</hibernate-mapping>
package zr.com.pojo;

public class Student {
    
    private int id;
    private String name;
    
    private TeamClass teamClass;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TeamClass getTeamClass() {
        return teamClass;
    }

    public void setTeamClass(TeamClass teamClass) {
        this.teamClass = teamClass;
    }

    public Student() {
        super();
    }

    public Student(int id, String name, TeamClass teamClass) {
        super();
        this.id = id;
        this.name = name;
        this.teamClass = teamClass;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", teamClass="
                + teamClass + "]";
    }
    
    
    
    
    
    

}
package zr.com.pojo;

import java.util.HashSet;
import java.util.Set;

public class TeamClass {
    
    private int classID;
    
    private String lfname;
    
    private Set<Student> students = new HashSet<Student>();

    public int getClassID() {
        return classID;
    }

    public void setClassID(int classID) {
        this.classID = classID;
    }

    public String getLfname() {
        return lfname;
    }

    public void setLfname(String lfname) {
        this.lfname = lfname;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    public TeamClass() {
        super();
    }

    public TeamClass(int classID, String lfname, Set<Student> students) {
        super();
        this.classID = classID;
        this.lfname = lfname;
        this.students = students;
    }

    @Override
    public String toString() {
        return "TeamClass [classID=" + classID + ", lfname=" + lfname
                + ", students=" + students + "]";
    }


}
package zr.com.test;

import java.util.List;
import java.util.UUID;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import zr.com.pojo.Student;
import zr.com.pojo.TeamClass;


public class TestClass {
    
    public static void main(String[] args) {
        
        // 创建Configuration 对象
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        // 创建ServiceRegistry 对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        // 创建SessionFactory 对象
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // 创建Session 对象
        Session session = sessionFactory.openSession();
        
        // 开启事务
        session.beginTransaction();
        // 创建实体类
        Student student1= new Student();
        student1.setName("lf");
        Student student2= new Student();
        student2.setName("gl");
        
        TeamClass teamClass = new TeamClass();
        teamClass.setLfname("java");
        teamClass.getStudents().add(student1);
        teamClass.getStudents().add(student2);
        // 插入数据
        session.save(teamClass);
        
        // 提交事务
        session.getTransaction().commit();
        
    
    }
}

 

hibernate 一对多(级联关系)