Hibernate--relational mapping

Statement: This post is only for personal study and summary, without any commercial use. If there is any infringement or problem, please contact as soon as possible, and I will delete the post immediately. 

 

Hibernate--relational mapping

 

      When designing a database, we will consider the relationship between tables, such as the one-to-one, one-to-many, and many-to-many relationships that we often mentioned earlier. In the database, we use foreign keys, the third table, etc. implement these relationships. And the mapping between the hibernate time entity class and the table in the database, how to map these relationships? Here is a brief summary:

 

 

one-to-many, many-to-one

 

Here is a simple demonstration with students and classes:

 

Student's class and corresponding mapping file

 

private int sid ;  
private String sname ;  
  
private Classes classes; //, introduce class objects, many-to-one

 

<class name="com.ljh.hibernate.pojo.Student" table="t_student" lazy="false">  
    <id name="sid" column="sid">  
        <generator class="native"/>  
    </id>  
    <property name="sname" column="sname" type="java.lang.String" length="20" not-null="true"/>         
      
    <!--   
        Represents the relationship of objects: many-to-one  
        name represents the relationship object of the current class  
        column represents the foreign key field in the database (also describes the data relationship)  
        class represents the type of the name attribute value  
        cascade  
            The operations performed by the active side (insert, update, delete), the passive side also follows the same operations.  
            Values: save-update, delete, all  
            save-update : cascade saving or updating associated objects when saving or updating the current object  
            delete : When deleting the current object, cascade delete related objects  
            all : Contains three operations: save, update, and delete.  
            For many-to-one situations, delete and all cannot be used in cascade, otherwise the integrity of the data relationship will be violated.  
        lazy : lazy loading   
            Delay initialization of object information and query the database when the object is used.   
            false : disable lazy loading  
            proxy : Use lazy loading (default value), and use cglib proxy to complete the extended function of lazy loading.  
            no-proxy : Do not use a proxy, complete lazy loading. Third-party bytecode enhancement tools can be used.  
        fetch : Data fetching strategy: according to the active side, the query method used to query the passive side.    
            The default value of fetch="select" will use multiple statements to find, often delay loading data  
            fetch="join" will use left join to query data by default, and will not delay loading data.  
                not-null If the value is true, the framework uses inner joins to query data.  
     -->  
    <many-to-one name="classes" column="cid" cascade="save-update" lazy="no-proxy" fetch="join" not-null="true" class="com.ljh.hibernate.pojo.Classes"></many-to-one>  
</class>  

 

 

The entity class of the class and the corresponding mapping file

 

private int cid ;  
private String cname ;  
  
private Set<Student> studentSet = new HashSet<Student>(); //Introduce a collection of student classes, one-to-many

 

<class name="com.ljh.hibernate.pojo.Classes" table="t_classes" >  
    <id name="cid" column="cid">  
        <generator class="native"/>  
    </id>  
    <property name="cname" column="cname" type="java.lang.String" length="20" not-null="true"/>     
      
    <!-- declares a one-to-many mapping  
        lazy : lazy loading  
            false : disable lazy loading  
            true : lazy loading (default)  
            extra : supports lazy loading. (recommend)  
                When obtaining information about the collection itself, efficient query statements can be sent.  
                For example, if you only want to obtain the length of the collection, but do not need to obtain the information of the data in the collection, the framework will execute the query through the function to calculate the length of the collection. .  
      
        fetch : data fetching strategy  
                The query method used when querying the data of the associated object.  
            join : immediate query through a join statement. (lazy loading doesn't work)  
            select : Query through multiple query statements.  
            subselect : Immediate query through sub-query statement. (deprecated) (same result as select by default)  
          
        not-null="true" For a one-to-many query, even if the not-null statement is set, the left join query is still used.  
          
        Inverse="true", which means inversion of control, and the foreign key is managed by the other side, that is, the student side. Because the foreign key is in the student    
     -->  
    <set name="studentSet" cascade="all" inverse="true" fetch="subselect">  
        <key column="cid" not-null="true"></key>  
        <one-to-many class="com.bjpowernode.hibernate.pojo.Student"/>  
    </set>  
</class>  

 

 

Self-association: It is to associate itself in its own class, such as the relationship between the parent menu and the submenu, the corresponding entity class, and the mapping file

 

private int mid ;           
private String name ;  
private Set<Menu> menuSet = new HashSet<Menu>(); //The relationship between parent menu and submenu is: one-to-many  
private Menu pmenu ; //The relationship between the sub menu and the parent menu is: many-to-one  

 

<class name="com.ljh.hibernate.pojo.Menu" table="t_menu" >  
    <id name="mid" column="mid">  
        <generator class="native"/>  
    </id>  
    <property name="name" column="name" type="java.lang.String" length="20" not-null="true"/>   
      
    <many-to-one name="pmenu" column="m_id" cascade="save-update"></many-to-one>  
      
    <!--  
        自关联表的设计:外键字段不能为非空。 
     -->  
    <set name="menuSet" cascade="all" inverse="true">  
        <key column="m_id"></key>  
        <one-to-many class="com.ljh.hibernate.pojo.Menu"/>  
    </set>  
</class>  

 

 

 

一对一

 

假如是主键一对一用来映射:也就是说被动方的主键是来自于主动方的主键,也可以将之称之为外键:

 

类之间相互添加彼此的应用。

映射文件中主动方,增加一对一标签:

 

<one-to-one name="userinfo" cascade="all" class="com.ljh.hibernate.pojo.UserInfo"></one-to-one>  
  
              被动方,主键又是外键,也添加一对一的映射标签:  
    <id name="uid" column="uid">  
        <generator class="foreign">  
            <param name="property">user</param>  
        </generator>  
    </id>  
      
    <!--   
        描述一对一关系关系映射  
        constrained="true" : 表示强制要求一对一使用外键关联。增加外键约束。  
     -->  
    <one-to-one name="user" constrained="true"  class="com.ljh.hibernate.pojo.User"></one-to-one>  
 

 

 

使用外键约束,其实是多对一的特殊情况,例如学生对班级

 

类添加彼此的应用。

 

映射文件中学生端主动端,添加外键进行约束,添加多对一标签

 

<!--   
    unique : 唯一约束  
      
        如果外键含有unique约束,那么表示主动方和被动方的关系为一对一。  
          
        对于一对一映射来讲,可以设置级联关系为delete 和 all  
          
        根据主动方查询被动方关联数据,是支持延迟加载的。  
 -->  
<many-to-one name="classes" column="cid" unique="true" cascade="all" class="com.ljh.hibernate.pojo.Classes"></many-to-one>  
  
               班级端的映射文件:添加一对一的标签:  
<one-to-one name="student" cascade="all" property-ref="classes" class="com.ljh.hibernate.pojo.Student"></one-to-one>  

 

 

 

 

多对多

                  

 

例如学生对课程的对应,在数据库中会生成第三张表进行维护,在各自的类中引入对方的set集合,表示多对多。

 

学生端:

<set name="courseSet" table="t_student_course" cascade="save-update">  
    <key column="sid"></key>  
    <many-to-many class="com.ljh.hibernate.pojo.Course" column="cid"></many-to-many>  
  
</set>  

 

课程端:

<!--   
    对于多对多来讲,级联只能设置cascade="save-update"是合理的  
    inverse="true" 让集合一端去维护中间表数据。任意一端都可以。  
 -->  
  
<set name="studentSet" table="t_student_course"  cascade="save-update"  inverse="true">  
    <key column="cid"></key>  
    <many-to-many class="com.ljh.hibernate.pojo.Student" column="sid"></many-to-many>  
</set>  

 

 

 

联合主键的映射,这种情况很少见

 

出现了这种联合键时,需要我们为其定义一个联合主键的类(实现Serializable接口),类中声明多个字段的属性,表示联合主键字段。

 

在配置映射文件时,主键的配置利用联合主键的标签即可:

<composite-id name="id">              
   <key-property name="title"></key-property>  
   <key-property name="author"></key-property>  
</composite-id>

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397773&siteId=291194637