• Hibernate ORM框架——续第二章:Hibernate映射关系:双向关联


    一、多对多(在双方实体中都加入一个Set)

    (1)HibernateUtil包映射文件不变

    (2)hibernate.cfg.xml配置文件基本不变:除了两个映射文件更换顺序会引起中间表的数据跟着改变。

    思考来源:中间表插入数据insert into 中间表(“类1”,“类2”);和insert into 中间表(“类2”,“类1”);顺序会不会变?

    (3.1)实体一:班级

    package shuang.m2m;
    
    import java.util.Set;
    
    public class ClassInfo {
        private String cid ;
        private String name;
        //一对多
        private Set<Person> persons;
    }

    (3.2)实体二:学生(人)

    package shuang.m2m;
    
    import java.util.Date;
    import java.util.Set;
    
    public class Person {
        private String pid ;//UUID
        private String name;
        private Set<ClassInfo> cis;
    }

    (4.1)班级实体映射文件

    <?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="shuang.m2m">
        <class name="ClassInfo" table="CLASSINFO">
            <id name="cid" column="cid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
    <!--
          set name="persons" :持久化属性名 table="classperson":中间表名
          <key column="rcid"></key>:与本实体关联的外键名
          <many-to-many column="rpid" class="Person"></many-to-many>:相关联实体的外键名以及类名
          -->
            <set name="persons" table="classperson">
                <key column="rcid"></key>
                <many-to-many column="rpid" class="Person"></many-to-many>
            </set>
        
        </class>
    </hibernate-mapping>

    (4.2)班级实体映射文件

    <?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="shuang.m2m">
        <class name="Person" table="persons">
            <id name="pid" column="pid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
            
            <set name="cis" table="classperson">
                <key column="rpid"></key>
                <many-to-many column="rcid" class="ClassInfo"></many-to-many>
            </set>
        </class>
        
    </hibernate-mapping>

    二、一对多、多对一(想象多的一方是学生,在其类中加private ClassInfo ci;一的一方是班级,在其类中加private Set<Person> persons;)

    (1)(2)HibernateUtil和hibernate.cfg.xml配置文件都是一样的

    (3.1)班级实体

    package shuang.m2oo2m;
    
    import java.util.Set;
    
    public class ClassInfo {
        private String cid ;
        private String name;
        //一对多
        private Set<Person> persons;
    }

    (3.2)学生实体

    package shuang.m2oo2m;
    
    import java.util.Date;
    
    public class Person {
        private String pid ;//UUID
        private String name;
        private ClassInfo ci; 
    }

    (4.1)班级映射文件

    <?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="shuang.m2oo2m">
        <class name="ClassInfo" table="CLASSINFO">
            <id name="cid" column="cid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
            
            <set name="persons">
                <key column="rci" ></key>
                <one-to-many class="Person"/>
            </set>
        </class>
    </hibernate-mapping>

    (4.2)学生映射文件

    <?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="shuang.m2oo2m">
        <class name="Person" table="persons">
            <id name="pid" column="pid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
            
            <many-to-one name="ci" column="rci"></many-to-one>
        </class>
    </hibernate-mapping>

    三、一对一(在双方类都加一个对方的类)

    (1)(2)HibernateUtil和hibernate.cfg.xml配置文件都是一样的

    (3.1)班级实体

    package shuang.o2o;
    
    import java.util.Set;
    
    public class ClassInfo {
        private String cid ;
        private String name;
        private Person p;
    }

    (3.2)学生实体

    package shuang.o2o;
    
    import java.util.Date;
    import java.util.Set;
    
    public class Person {
        private String pid ;//UUID
        private String name;
        private ClassInfo c;
    }

    (4.1)班级映射文件

    <?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="shuang.o2o">
        <class name="ClassInfo" table="CLASSINFO">
            <id name="cid" column="cid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
            <!-- 我们心中认为这个类是一的一方,也就是其对应的表
                没有外键,在此配置一定要加property-ref已确定自己的主键
                与对方哪一列建立联系
             -->
            <one-to-one name="p" property-ref="c"></one-to-one>
        </class>
    </hibernate-mapping>

    (4.2)学生映射文件

    <?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="shuang.o2o">
        <class name="Person" table="persons">
            <id name="pid" column="pid">
                <generator class="assigned"></generator>
            </id>
            <property name="name" ></property>
            
            <many-to-one name="c" column="rcid" unique="true"></many-to-one>
        </class>
    </hibernate-mapping>

    四、个人总结

    五、数据库中MySQL语句中,的写法总结: 

    (1)不管是单向还是多向的一对一,都是需要在SQL语句中加上唯一约束语句

    (2)多对多中间多了一张中间表,与有两个外键约束

    (3)其余的都是通用多对一的SQL语句

    /*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/

  • 相关阅读:
    创建线程的多种方式
    第六届蓝桥杯大赛个人赛省赛(软件类)C语言B组--2015年
    第七届蓝桥杯大赛个人赛省赛(软件类)C语言B组--2016年
    postgressql安装
    freebsd上安装nginx+php记录
    ubuntu cpus 共享打印
    查找表_leetcode219
    查找表_leetcode217
    查找表_leetcode202
    查找表_leetcode205
  • 原文地址:https://www.cnblogs.com/Sunny-lby/p/7373263.html
Copyright © 2020-2023  润新知