首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

hibernate的照射关系

2012-11-22 
hibernate的映射关系Hibernate 映射1.)基本映射基本属性映射property namePO属性名 column数据库表

hibernate的映射关系
Hibernate 映射
    1.)基本映射
        基本属性映射
     <property name="PO属性名" column="数据库表列名" type="数据类型" />
     
    2.)集合映射   知道是如何实现 加一个表利用主键关联
  1.List
  <list name="list属性名" table="list对应表名">
   <key column="list属性所在类的id(主键" not-null="true"></key>
   <list-index column="list_order" />
   <element type="list元素类型" column="list元素列名"></element>
  </list>
2.Set
  <set name="set属性名" table="set对应表名">
   <key column="set属性所在类的id(主键" not-null="true"></key>
   <element type="set元素类型" column="set元素列名"></element>
  </set>
3.Map
  <map name="map属性名" table="map对应表名">
   <key column="map属性所在类的id(主键)" not-null="true"></key>
   <map-key type="map的key的数据类型" column="map的key对应的列名"></map-key>
   <element type="map的value的数据类型" column="map的value对应的列名"></element>
  </map>
    3.)关联映射  重点掌握 一对多   级联保存,更新 以及删除
         单向映射和双向映射的区别:
  单向:一方持有另一方的实例或集合  我中有你,你中没有我。也就是说:通过这个表可以查询到另外一个表数据,但是通
过另外一个表,查询不到这个表的数据
  双向:彼此持有对方的实例或集合    我中有你,你中也有我。也就是说:通过这个表可以查询到另外一个表的数据,通过
另外一个表也可以查询到这个表的数据        
  1对1
         <1>外键关联实现
        单向: <many-to-one name="对象属性名" column="字段名(可不写则与属性名相同)" unique="true"/>
                      双向: <many-to-one name="对象属性名" column="字段名(可不写则与属性名相同)" unique="true"/>
               <one-to-one name="对象属性名" property-ref="另一对象属性名"/>
             
  <2>主键关联实现   一对一主键关联映射中,默认了cascade属性
        单向:
        <id name="id">
   <generator constrained="true"/>
        双向:
        
         <id name="id">
   <generator constrained="true"/>
  
             
      
       另一方
       <one-to-one name="对象属性名"/>
      

1对多
               <1>单向:
  <set name="set属性名">
   <key column="外键列名"/>
   <one-to-many column="字段名(可不写则与属性名相同"/>
 
多对多  中间表
        <1>单向:
         <set name="addresses" table="jointable">
   <key column="personId"/>
   <many-to-many column="addressId" table="jointable">
   <key column="personid"/>
   <many-to-many column="wifeid" inverse="true" table="jointable">
   <key column="wifeid"/>
   <many-to-many column="personid" inverse="false" cascade="save-update,delete">
   <key column="外键列名"/>
   <one-to-many table="sup">
     <id name="id" column="id" type="java.lang.String">
      <generator />
     </id>
     <property name="name" column="name" type="java.lang.String" />
     <joined-subclass name="test.Sub1" table="sub1">
            <key column="id" />
      <property name="sex" type="java.lang.String" />
     </joined-subclass>
     <joined-subclass name="test.Sub2" table="sub2">
     <key column="id" />
      <property name="age" type="java.lang.String" />
     </joined-subclass>
    </class>
   </hibernate-mapping>

  <2>每个子类(具体类)映射一个表   (表个数与子类个数相同)
   <hibernate-mapping>
    <class name="test.Sup" table="sup" abstract="true">
     <id name="id" column="id" type="java.lang.String">
      <generator />
     </id>
     <property name="name" column="name" type="java.lang.String" />
     <union-subclass name="test.Sub1" table="sub1" >
      <property name="sex" type="java.lang.String" />
     </union-subclass>
     <union-subclass  name="test.Sub2" table="sub2">
      <property name="age" type="java.lang.Integer" />
     </union-subclass>
    </class>
   </hibernate-mapping>

  <3>所有类用一个表映射
   <hibernate-mapping>
    <class name="test.Sup" table="sup" discriminator-value="sup">
     <id name="id" column="id" type="java.lang.String">
      <generator />
     </id>
     <discriminator column="personType" type="java.lang.String" /><!--在id属性标签后-->
     <property name="name" column="name" type="java.lang.String" />
     <subclass name="test.Sub1" discriminator-value="sub1">
      <property name="sex" type="java.lang.String" />
     </subclass>
     <subclass name="test.Sub2" discriminator-value="sub2">
      <property name="age" type="java.lang.Integer" />
     </subclass>
    </class>
   </hibernate-mapping>
        三种方法的优缺点:
            1、每一个具体子类映射成单个数据库表,而抽象基类不参与映射。
                优点:
                数据操作实现简单,每个表中都包含自己所需要的具体子类的所有信息,减少了多表关联操作时的性能消耗。
                缺点:
                类的修改会导致相对应的表及其子类所对应表的更改。不支持多态查询。
                应用:
                适合在类层次结构上有一定数量的抽象类的情况下使用。
            2、将整个类层次映射为单个数据库表。
                这对于子类属性不多的情况非常有效。每个子类由识别列(discriminator column)区分。
                优点:
                实现简单,并支持多态。同时数据访问也比较简单,因为数据库表中包含了所有需要的信息。
                缺点:
                增加类层次中的耦合,类层次中任何类的属性的增加都有会导致表的变更。另外,对子类属性的修改错误将会影响到整个类
的层次结构。当然也浪费了大量的数据库空间。表中引入区分子类的字段,子类的字段不能创建为空。
            3、继承关系中每个类均映射为一个数据库表
                优点:
                此时,与面向对象的概念是一致的,这种映射实现策略的最大好处就是关系模型完全标准化,关系模型和领域模型完全一致
,易于修改基类和增加新的子类。
                缺点:
                数据库中存在大量的表,为细粒度级的数据模型,访问数据时将存在大量的关联表的操作,效率较低。
  5.)组件映射
            在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,
         component可以成为是值对象(DDD)
           采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高
    
     <component name="contact(Javabean属性名)">
   <property name="email"/>
   <property name="address"/>
   <property name="zipCode"/>
   <property name="contactTel"/>
  </component>
   6.)组合映射(不常用)
         通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法

热点排行