• hibernate映射文件


    映射文件结构

    映射文件的根元素为hibernate-mapping元素,可以拥有多个<class />子元素,每个<class />子元素对应一个持久化类的映射, 被映射的类必须要有一个id定义。

       <hibernate-mapping  package=“”>

          <class/>

          <class/>

       </hibernate-mapping>

    1  Package属性

    表示包名,可省略,省略时书写类名时需要写全类名。

     

    2  class元素

    常用属性

    • name – 实体类的类名
    • table – 被映射到数据库表的名称

    可以包含的常见子元素

    • id – 主键定义
    • property – 属性定义
    • 关系映射定义(一对多、多对一等)

    3  主键-id映射

    <id

            name="propertyName"                     (1)

            type="typename"                          (2)

            column="column_name"                    (3)

            unsaved-value="any|none|null|id_value“>     (4)

             <generator class="generatorClass"/>           (5)

    </id>

    1)   name (可选) :标识属性的名称。

    2)   type(可选):标识Hibernate类型的名字。

    3)   column(可选-默认为属性名):对应数据库表的主键字段的名字。

    4)   unsaved-value(可选-默认为null):这个值用来判断对象是否要保存。

    5)   主键生成方式。

    4  主键生成方式

    • assigned

    主键由应用逻辑产生,数据交由Hibernate保存时,主键值已经设置完成,无需Hibernate干预。

    •  hilo

    通过hi/lo高低位算法实现的主键生成机制,需要额外的数据库表保存主键生成历史状态

    • seqhilo

    与hilo类似,通过hilo算法实现主键生成机制,生成long、short、int类型的标识符,只是主键历史状态保存在Sequence中,适用于支持Sequence的数据库,如Oracle。

    • increment

    主键按数值顺序递增。为long、short、int类型主键生成唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用。

    • identity

     在DB2MySQLSQL ServerSybaseHypersonicSQL等提供identity(自增长)主键支持的数据表中适用。返回的标识属性是long、short、int类型。

    • sequence

    采用数据库提供的sequence机制生成主键,如Oracle Sequence。返回的是long、short、int型

    • native

    由Hibernate根据数据库适配器中的定义,自动采用identity、hilo、sequence的其中一种作为主键生成方式(如果需要返回long、shor、int生成值,则推荐使用该策略)。

    • uuid

    由Hibernate基于128位唯一值产生算法,根据当前设备IP,时间,JVM启动时间,内部自增量等4个参数生成十六进制数值(编码后以长度为32位的字符串表示)作为主键。利用uuid.hex方式生成主键将提供最好的数据插入性能和数据库平台适应性。因为uuid生成的字符串在一个网络中是唯一的。故如果为String型生成值则推荐使用该策略。

    5  属性/字段映射

    5.1普通属性映射

    <property

            name="propertyName"                   (1)

            column="column_name"                  (2)

            type="typename"                       (3)

            update="true|false"                      (4)

            insert="true|false"                       (4)

            formula=“arbitrary SQL expression”       (5)

    />

    (1) name:指定了映射类中的属性名为” propertyName”,此属性将被映射到指定的库表字段。

    (2) column(可选):指定了库表中对应映射类属性的字段名。

    (3) type(可选):指定了映射字段的数据类型

    (4) update, insert (可选 - 默认为 true) :表明在用于UPDATE 和/或 INSERT的SQL语句中是否包含这个字段。

    (5) formula (可选): 一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。

    5.2 集合属性映射

    5.2.1  List

    • List是有序集合,因此持久化到数据库时必须增加一列来表示集合元素的次序。
    • 集合属性只能以接口声明(当持久化某个实例时, Hibernate 会自动把程序中的集合实现类替换成 Hibernate 自己的集合实现类),因此下面代码中,schools的类型能是List,不能是ArrayList

    案例:

    <!-- 映射List集合属性-->
    
    <list name="schools" table="person_school">
    
      <!-- 映射集合属性数据表的外键列 -->
    
      <key column="person_id" not-null="true"></key>
    
        <!-- 映射集合属性数据表的集合索引列 -->
    
      <list-index column="list_order"></list-index>
    
      <!-- 映射保存集合元素的数据列 -->
    
      <element type="string" column="school_name"></element>
    
    </list>

    5.2.2 Set

    • Set是无序,不可重复的集合

    案例:

    <!--映射Set集合属性-->
    
    <set name="interests" table="person_interest">
    
        <!-- 映射集合属性数据表的外键列 -->
    
        <key column="person_id" not-null="true"/>
    
        <!-- 映射保存集合元素的数据列 -->
    
        <element type="string" column="interest" not-null="true"/>
    
    </set>

    5.2.3 Map

    • Map不仅需要映射属性值,还需要映射属性Key。
    • Hibnernate 将以外键列和Key列作为联合主键。
    • Map集合属性使用map元素映射,该元素需要key和map-key两个子元素
      • key子元素用于映射外键列,
      • map-key子元素则用于映射Map集合的Key。
      • map-key和element元素都必须确定type属性

    案例:

    <!--映射Map集合属性-->
    
    <map name="scores" table="student_score">
    
      <!-- 映射外键列 -->
    
      <key column="student_id" not-null="true"></key>
    
      <!-- 映射Map集合的Key -->
    
      <map-key type="string" column="subject" length="10"></map-key>
    
      <!-- 映射Map集合的Value -->
    
      <element type="double" column="score" not-null="true"></element>
    
    </map>

    部分程序模板:

    public class News {
    
             private int id;
    
             private String title;
    
             private String context;
    
             private BigDecimal aa;
    
             private List listColumn;
    
             private String formulaTest;
    
             private Set editor;
    
        //get/set……
    
    }
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    
    <hibernate-mapping >
    
             <class name="com.silvan.pojo.News" table="t_news" lazy="false">
    
                       <id name="id" column="id" >
    
                                <generator class="native"></generator>
    
                       </id>
    
                       <property name="title" column="t_title" lazy="false"></property>
    
                       <property name="context" column="t_context"></property>
    
                       <property name="aa" type="big_decimal" precision="8" scale="2"></property>
    
                       <property name="formulaTest" formula="(select concat(t.t_title,t.t_context) from t_news t where t.id=id)"></property>
    
                       <list name="listColumn" table="listColumnTable">
    
                                <key column="list_id" not-null="true"></key>
    
                                <list-index column="list_order"></list-index>
    
                                <element type="string" column="list_name"></element>
    
                       </list>
    
                       <set name="editor" table="t_editor" lazy="true">
    
                                <key column="id"></key>
    
                                <element column="editor_name" type="string"></element>
    
                       </set>
    
             </class>
    
    </hibernate-mapping>
  • 相关阅读:
    application manifest 中的requestedElevationLevel令牌
    【转载】讲故事谈.NET委托:一个C#睡前故事
    Sql Server排名函数
    SQL Server窗口函数的简单使用
    【转载】 [Server Error in Application ] WCF and HTTP Error 404.3 Not Found
    SystemColors.ActiveCaptionText在不同操作系统中的颜色不同
    问题:Cannot execute a program. The command being executed was "C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe"
    【转载】WCF and HTTP Error 404.3 Not Found
    【转载】cross apply & outer apply
    使用foreach来取代其它的循环结构
  • 原文地址:https://www.cnblogs.com/zhouyeqin/p/7194469.html
Copyright © 2020-2023  润新知