Hibernate总结(二)

关联关系映射
  • 多对一映射:在多的一段维护关联关系.例如:一个user属于属于group中.

 Group
< hibernate-mapping package="com.oracle.po">

    < class name="Group" table="tb_group" >
        < id name="id" type="java.lang.Integer">
          < column name="id">
          < generator class="native">< /generator>
        < /id>
    < property name="name" length="30">< /property>
        < !--inverse 把维护端交给另一端;不进行维护关系,不会更新外键;  -->
       < set name="users" inverse="true">
           < key column="group_id">
          < one-to-many class="User"/>
        
    < /class>


User 
< hibernate-mapping package="com.oracle.po">
< class name="User" table="tb_user" >
< id name="id" type="java.lang.Integer">
< column name="id">
< generator class="native">
< /id>
< property name="name" type="string" length="30"/>
        < !-- 
           all : 所有情况下均进行关联操作。
           none:所有情况下均不进行关联操作。这是默认值。
           save-update:在执行save/update/saveOrUpdate时进行关联操作。
           delete:在执行delete时进行关联操作

         -->
        < many-to-one name="group" column="group_id" cascade="save-update">
    < /class>


上面配置的是多对一的双向关联,可以双向的

  • 一对一映射:例如人和身份证,有两种关系,主键关联和唯一外键关联

    唯一外键关联:就是一种特殊的多对一


Person
< hibernate-mapping package="com.oracle.po">    
    < class name="Person" table="tb_Person" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">< /generator>
        < /id>
        < property name="name" length="30">< /property>
        < many-to-one name="idCard" unique="true" cascade="all">
    < /class>

< /hibernate-mapping>

IdCard
< hibernate-mapping package="com.oracle.po">    
    < class name="IdCard" table="tb_idcard" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">
        < /id>
        < property name="cardNo" type="string" length="18"/>

    < /class>

< /hibernate-mapping>

主键关联映射:


Person
< hibernate-mapping package="com.oracle.po">    
    < class name="Person" table="tb_Person" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">< /generator>
        < /id>
        < property name="name" length="30">< /property>

    < /class>

< /hibernate-mapping>

< hibernate-mapping package="com.oracle.po">
    < class name="IdCard" table="tb_idcard" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">< /generator>
        < /id>
        < property name="cardNo" type="string" length="18"/>
        < one-to-one name="person" property-ref="idCard">
    < /class>

< /hibernate-mapping>
  • 一对多映射,有少的一段来维护映射关系.

< hibernate-mapping package="com.oracle.po">
    < class name="User" table="tb_user" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">
        < /id>
        < property name="name" type="string" length="30"/>

    < /class>

< /hibernate-mapping>

< hibernate-mapping package="com.oracle.po">    
    < class name="Person" table="tb_Person" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="foreign">
              < param name="property">idCard
           < /generator>
        < /id>
        < property name="name" type="string">< /property>
        < one-to-one name="idCard" constrained="true">

    < /class>

< /hibernate-mapping>
  • 一对一的双向映射:

    主键一般是共和:


Person端
< hibernate-mapping package="com.oracle.po">

    < class name="Person" table="tb_Person" >
        < id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="foreign">
              < param name="property">idCard
           < /generator>
        < /id>
        < property name="name" type="string">< /property>
        < one-to-one name="idCard" constrained="true" cascade="all" >
或者
< many-to-one> < /many-to-one>


    < /class>

< /hibernate-mapping>

IdCard端
< hibernate-mapping package="com.oracle.po">

    < class name="IdCard" table="tb_idcard" >
         id name="id" type="java.lang.Integer">
            < column name="id">
           < generator class="native">
        < /id>
        < property name="cardNo" type="string" length="18"/>
        < one-to-one name="Person"   class="Person" constrained="true" cascade="all" >
或者
< many-to-one> < /many-to-one>

    < /class>

< /hibernate-mapping>

kxind

性别:男. 敢于尝试 爱折腾的死宅程序猿. https://github.com/kxinds