Hibernate的映射文件(hbm.xml)属性说明

Hibernate的映射文件(hbm.xml)属性说明

1.class 元素

name: 类名

table: 类对应表名,默认为类名称

dynamic-update: 生成更新字段时,只包含发生变动的字段,默认为false。

dynamic-insert: 生成insert语句时仅包含非null字段

Proxy: 代理类,默认为空

discriminator-value: 子类辨别标识用于多态支持

where: 通过限定条件查询结果集。如:查询有籍在校学生的信息可以使用"where studentstatus='0'"

------------------------------------------------------------------------------------------------------------------

2.id 元素

1.column                     字段名称

2.type                            字段类型

3.length                        字段长度

4.unsaved-value         用于判断对象值是否已经保存

5.name                         类里面的属性名

6.generator class       主键产生方式

                        assigned

                        hilo

                        seqhilo

                        increment

                        identity

                        sequence

                        native

                        uuid.hex

                        uuid.string

                        foreign

---------------------------------------------------------------------------------------------------------------

主键产生方式说明

increment(递增)

用于为long, short或者int类型生成唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用。 在集群下不要使用。

identity

对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。返回的标识符是long, short 或者int类型的。

sequence (序列)

在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列(sequence),而在Interbase中使用生成器(generator)。返回的标识符是long, short或者 int类型的。

hilo (高低位)

使用一个高/低位算法来高效的生成long, short或者 int类型的标识符。给定一个表和字段(默认分别是是hibernate_unique_key 和next_hi)作为高位值得来源。高/低位算法生成的标识符只在一个特定的数据库中是唯一的。在使用JTA获得的连接或者用户自行提供的连接中,不要使用这种生成器。

seqhilo(使用序列的高低位)

使用一个高/低位算法来高效的生成long, short或者 int类型的标识符,给定一个数据库序列(sequence)的名字。

uuid.hex

用一个128-bit的UUID算法生成字符串类型的标识符。在一个网络中唯一(使用了IP地址)。UUID被编码为一个32位16进制数字的字符串。

uuid.string

使用同样的UUID算法。UUID被编码为一个16个字符长的任意ASCII字符组成的字符串。不能使用在PostgreSQL数据库中

native(本地)

根据底层数据库的能力选择identity, sequence 或者hilo中的一个。

assigned(程序设置)

让应用程序在save()之前为对象分配一个标示符。

说明主键是自己定的,不是数据库生成的,用户注册的帐号一般就属于这种类型。

foreign(外部引用)

-------------------------------------------------------------------------------------------------------------------------

3.property 元素

1.column                数据库表字段名称

2.type                  类型

3.length                长度

4.not-null              字段是否允许为空

5.unique                字段是否允许唯一(是否允许重复值)

6.insert                insert操作时,是否允许包含本字段数值

7.update                update操作时,是否包含本字段数据

====================================================================

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/Hibernate Mapping DTD 2.0//EN"

                    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

   <class name="com.oreilly.hh.Track" table="TRACK">

     <meta attribute="class-description">

        Represents a single playable track in the music database.

        @author Jim Elliot(with help from Hibernate)

     </meta>

    

     <id name="id" type="int" column="TRACK_ID">

       <meta attribute="scope-set">protected</meta>

          <generator class="native"/>

     </id>

    

     <property name="title" type="string" not-null="true"/>

    

     <property name="filePath" type="string" not-null="true"/>

    

     <property name="playTime" type="time">

        <meta attribute="field-description">Playing time</meta>

     </property>

    

     <property name="added" type="date">

        <meta attribute="field-description">When the track was created</meta>

     </property>

    

     <property name="volume" type="short" not-null="true">

        <meta attribute="field-description">How loud to play the track</meta>

     </property>

    

   </class>

</hibernate-mapping>

     说明如下:

1.<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/Hibernate Mapping DTD 2.0//EN"

                    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

用于导言说明,说明它的文件格式定义。

2.<hibernate-mapping>标签里是真正的映射。

3.<class name="com.oreilly.hh.Track" table="TRACK">

定义一个类com.oreilly.hh.Track的映射。(可以定义任意多个类在一个映射文件里)。表示存在数据库表TRACK中。

4. <meta attribute="class-description">

        Represents a single playable track in the music database.

        @author Jim Elliot(with help from Hibernate)

     </meta>

定义了说明,可以被JavaDoc读取。

5. <id name="id" type="int" column="TRACK_ID">

       <meta attribute="scope-set">protected</meta>

          <generator class="native"/>

     </id>

定义了类属性和数据库表列的映射。   <generator class="native"/>是表示ID生成策略,此种策略有多种。(请查看“主键产生方式说明”部分)

6. <property name="volume" type="short" not-null="true">

        <meta attribute="field-description">How loud to play the track</meta>

  </property>                                                                                                                                             

定义了说明,可以被JavaDoc读取。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值