美文网首页
Hibernate之对象关系

Hibernate之对象关系

作者: 紫荆秋雪_文 | 来源:发表于2019-09-29 00:05 被阅读0次

    一、常用关系

    依赖关系

    如果A对象离开了B对象,A对象就不能正常编译,则A对象依赖B对象

    class  B{}
    
    class  A{
         //在A类中使用到了B.
         B b;//依赖情况1
         public void doWok(B  b)//依赖情况2
         { 
                B bb;//依赖情况3
          }
    }
    

    关联关系

    A对象依赖B对象,并且把B对象作为A对象的一个属性,则A和B是关联关系,关联关系是一种特殊的依赖关系

    聚合关系

    表示整体和部分的关系,整体和部分之间可以相互独立存在,一定是有两个模块来分别管理整体和部分

    组合关系(强聚合关系)

    是整体和部分不能独立存在,一定是在一个模块中同时管理整体和部分,生命周期必须相同,如:订单和订单明细

    二、多重性

    一对一

    一个A对象属于一个B对象,一个B对象属于一个A对象。比如:每一个人有一个唯一的省份证号

    • Person
    package com.revanwang._02.relation.one2one;
    
    import lombok.Data;
    
    @Data
    public class Person {
        private IDNumber idNumber;
    }
    
    • IDNumber
    package com.revanwang._02.relation.one2one;
    
    import lombok.Data;
    
    @Data
    public class IDNumber {
        private Person person;
    }
    
    • testOne2One
        public void testOne2One() {
            Person person = new Person();
            IDNumber idNumber = new IDNumber();
    
            person.setIdNumber(idNumber);
            idNumber.setPerson(person);
        }
    

    一对多

    一个A对象包含多个B对象(一个部门有多个员工)

    • Department
    package com.revanwang._02.relation.one2many;
    
    import lombok.Getter;
    import lombok.Setter;
    
    import java.util.List;
    
    public class Department {
    
        @Setter
        @Getter
        private List<Employee> employeeList = new ArrayList<>();
    
    }
    
    • Employee
    package com.revanwang._02.relation.one2many;
    
    public class Employee {
    }
    
    • testOne2Many
        public void testOne2Many() {
            Department depart = new Department();
            Employee e1 = new Employee();
            Employee e2 = new Employee();
            Employee e3 = new Employee();
    
            depart.getEmployeeList().add(e1);
            depart.getEmployeeList().add(e2);
            depart.getEmployeeList().add(e3);
        }
    

    多对一

    多个A对象属于一个B对象,并且每个A对象只能属于一个B对象(Employee 和 Department)

    • Employee
    package com.revanwang._02.relation.many2one;
    
    public class Employee {
    
        private Department department;
    }
    
    • Department
    package com.revanwang._02.relation.many2one;
    
    public class Department {
    }
    
    • testMany2One
        public void testMany2One() {
            Department department = new Department();
    
            Employee e1 = new Employee();
            Employee e2 = new Employee();
    
            e1.setDepartment(department);
            e2.setDepartment(department);
        }
    

    多对多

    一个A对象属于多个B对象,一个B对象属于多个A对象.

    • Teacher
    package com.revanwang._02.relation.many2many;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Data
    public class Teacher {
    
        private List<Student> studentList = new ArrayList<>();
    
    }
    
    • Student
    package com.revanwang._02.relation.many2many;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Data
    public class Student {
    
        private List<Teacher> teacherList = new ArrayList<>();
    
    }
    
    • testMany2Many
        public void testMany2Many() {
            Teacher t1 = new Teacher();
            Teacher t2 = new Teacher();
    
            Student s1 = new Student();
            Student s2 = new Student();
    
            s1.getTeacherList().add(t1);
            s1.getTeacherList().add(t2);
    
            t1.getStudentList().add(s1);
            t1.getStudentList().add(s2);
        }
    

    三、导航性

    如果通过A对象中的某一个属性可以访问该属性对应的B对象,则说A可以导航到B.

    • 1).单向:只能从A通过属性导航到B,B不能导航到A.
    • 2).双向:A可以通过属性导航到B,B也可以通过属性导航到A.
      判断方法:
      1,判断都是从对象的实例上面来看的;
      2,判断关系必须确定一对属性;
      3,判断关系必须确定具体需求;

    四、集合映射

    4.1、Set映射

    • Person
    package com.revanwang._03.collection.set;
    
    import lombok.Data;
    
    import java.util.HashSet;
    import java.util.Set;
    
    
    @Data
    public class Person {
        private Long id;
        private String name;
        private Set<String> hobbySet = new HashSet<>();
    }
    
    • Person.hbm.xml
    <?xml version="1.0"?>
    
    <!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.revanwang._03.collection.set">
    
        <class name="Person" table="t_person">
            <id name="id" column="id">
                <generator class="native"></generator>
            </id>
    
            <property name="name" column="name"/>
            
            <!-- Set集合设置 -->
            <set name="hobbySet" table="person_hobby_set">
                <key column="p_id"></key>
                <element column="hobby" type="java.lang.String"/>
            </set>
        </class>
    
    </hibernate-mapping>
    
    • testSet
        public void testSet() {
            Person person = new Person();
            person.setName("乔峰");
            person.getHobbySet().add("喝酒");
            person.getHobbySet().add("天龙八部");
            person.getHobbySet().add("北乔峰");
    
            Session session = HibernateUtil.getHibernateSession();
            session.beginTransaction();
            session.persist(person);
            session.getTransaction().commit();
        }
    
    t_person表.png set表.png

    4.2、List映射

    • Person
    package com.revanwang._03.collection.list;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    @Data
    public class Person {
        private Long id;
        private String name;
        private List<String> hobbyList = new ArrayList<>();
    }
    
    • Person.hbm.xml
    <?xml version="1.0"?>
    
    <!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.revanwang._03.collection.list">
    
        <class name="Person" table="t_person">
            <id name="id" column="id">
                <generator class="native"></generator>
            </id>
    
            <property name="name" column="name"/>
    
            <!-- List集合设置 -->
            <list name="hobbyList" table="person_hobby">
                <key column="p_id"></key>
                <index column="ind" type="integer"/>
                <element column="hobby" type="java.lang.String"/>
            </list>
        </class>
    
    </hibernate-mapping>
    

    4.3、Bag映射

    • Person
    package com.revanwang._03.collection.bag;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    @Data
    public class Person {
        private Long id;
        private String name;
        private List<String> hobbyList = new ArrayList<>();
    }
    
    • Person.hbm.xml
    <?xml version="1.0"?>
    
    <!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.revanwang._03.collection.bag">
    
        <class name="Person" table="t_person">
            <id name="id" column="id">
                <generator class="native"></generator>
            </id>
    
            <property name="name" column="name"/>
    
            <!-- Bag集合设置 -->
            <bag name="hobbyList" table="t_person_bag">
                <key column="p_id"></key>
                <element column="hobby" type="java.lang.String"/>
            </bag>
        </class>
    
    </hibernate-mapping>
    

    相关文章

      网友评论

          本文标题:Hibernate之对象关系

          本文链接:https://www.haomeiwen.com/subject/uprductx.html