• hibernate基础06:关联关系之双向关联多对一


    1、Java实体bean类

    package com.project.pojo;
    
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Classes implements Serializable{
        private int id;
        private String name;
        private String address;
        private Set<Student> student = new HashSet<>();
        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 String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public Set<Student> getStudent() {
            return student;
        }
        public void setStudent(Set<Student> student) {
            this.student = student;
        }
        public void setStudent(Student student) {
            this.getStudent().add(student);
        }
        
    }
    package com.project.pojo;
    
    import java.io.Serializable;
    
    public class Student implements Serializable{
        private int id;
        private String name;
        private int age;
        private Classes classes;
        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 int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Classes getClasses() {
            return classes;
        }
        public void setClasses(Classes classes) {
            this.classes = classes;
        }
        
    }

    2、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.project.pojo">
        <class name="Classes" table="t_class">
            <id name="id" column="id" type="int">
                <generator class="native"></generator>
            </id>
            <property name="name" />
            <property name="address" />
            <set name="student" inverse="true">
                <key column="classes_id" />
                <one-to-many class="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">
    <!-- package指定类所在的包,如果不指定,那么在指定class的name时,需要指定完全限定名 -->
    <hibernate-mapping package="com.project.pojo">
        <class name="Student" table="t_student">
            <id name="id">
                <generator class="native"></generator>
            </id>
            <property name="name" />
            <property name="age" />
            <many-to-one name="classes" column="classes_id" />
        </class>
    
    </hibernate-mapping>

    3、hibernate.cfg.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>
            <!-- 1、数据库连接信息 -->
            <!-- 指定数据方言 -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://192.168.1.59:3306/hibernate?characterEncoding=UTF8</property>
            <property name="connection.username">root</property>
            <property name="connection.password">1234</property>
            
            <!-- 2、通用配置信息 -->
            <!-- 打印sql语句 -->
            <property name="show_sql">true</property>
            <!-- 格式化sql语句 -->
            <property name="format_sql">true</property>
            
            <!-- 
                hbm2ddl.auto 该属性用于配置是否将映射信息转换为ddl
                create 每次执行都会先删除后再创建表,用于测试
                update 检查数据库表是否存在,如果不存在则先创建表,项目中通常上线使用
             -->
            <!-- <property name="hbm2ddl.auto">create</property> -->
            <!-- 映射文件信息 -->
            <mapping resource="com/project/pojo/Classes.hbm.xml" />
            <mapping resource="com/project/pojo/Student.hbm.xml" />
        </session-factory >
    </hibernate-configuration>

    4、测试

    package com.project.test;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.project.pojo.Classes;
    import com.project.pojo.Student;
    import com.project.util.HibernateUtil;
    
    public class HibernateTest {
        Session session = null;
        Transaction ts = null;
        
        @Before
        public void setUp(){
            session = HibernateUtil.getSession();
            ts = session.beginTransaction();
        }
        
        @After
        public void tearDown(){
            HibernateUtil.closeSession();
        }
        
        @Test
        public void testCreateDB(){
            Configuration cfg = new Configuration().configure();
            //使得hibernate映射信息转换为数据库识别的dll语言
            SchemaExport se = new SchemaExport(cfg);
            //第一个参数:是否打印dll语句
            //第二个参数:是否将dll到数据库中执行
            se.create(true, true);
        }
        
        @Test
        public void testSave(){
            try {
                Classes c1 = new Classes();
                c1.setName("一年级");
                c1.setAddress("B-1栋");
                
                Classes c2 = new Classes();
                c2.setName("二年级");
                c2.setAddress("B-2栋");
                
                session.save(c1);
                session.save(c2);
                
                Student s1 = new Student();
                s1.setName("张三");
                s1.setAge(20);
                s1.setClasses(c1);
                
                Student s2 = new Student();
                s2.setName("李四");
                s2.setAge(21);
                s2.setClasses(c2);
                
                Student s3 = new Student();
                s3.setName("哈哈狗");
                s3.setAge(19);
                s3.setClasses(c1);
                
                session.save(s1);
                session.save(s2);
                session.save(s3);
                
                ts.commit();
            } catch (Exception e) {
                e.printStackTrace();
                if(ts!=null) ts.rollback();
            }
        
        }
        
        @Test
        public void testSelect(){
            Student s = (Student)session.get(Student.class, 1);
            System.out.println(s.getName()+"	"+s.getAge());
            System.out.println("---------------------");
            System.out.println(s.getClasses().getName()+"	"+s.getClasses().getAddress());
            System.out.println("=====================");
            Classes c = (Classes)session.get(Classes.class, 2);
            System.out.println(c.getName()+"	"+c.getAddress());
            System.out.println("---------------------");
            for(Student stu : c.getStudent()){
                System.out.println(stu.getName()+"	"+stu.getAge());
            }
        }
    }
  • 相关阅读:
    深入浅出数据库索引原理
    Mysql读写分离原理及主众同步延时如何解决
    数据库连接池实现原理
    MySQL 大表优化方案(长文)
    js-ajax-03
    js-ajax-04
    js-ajax-02
    js-ajax-01
    获取html对象方式
    js-事件总结
  • 原文地址:https://www.cnblogs.com/chai-blogs/p/12933867.html
Copyright © 2020-2023  润新知