天天看点

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.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读取。

继续阅读