创建数据库schema 在本例中,与customer类对应的数据库表名为customers,它在mysql数据库中的ddl定义如下:
create table customers (
id bigint not null primary key,
name varchar(15) not null,
email varchar(128) not null,
password varchar(8) not null,
phone int ,
address varchar(255),
sex char(1) ,
is_married bit,
description text,
image blob,
birthday date,
registered_time timestamp
);
customers表有一个id字段,它是表的主键,它和customer类的id属性对应。customers表中的字段使用了各种各样的sql类型,参见表2-2。
表2-2 customers表的字段使用的sql类型


2.4 创建对象-关系映射文件
hibernate采用xml格式的文件来指定对象和关系数据之间的映射。在运行时,hibernate将根据这个映射文件来生成各种sql语句。在本例中,将创建一个名为customer.hbm.xml的文件,它用于把customer类映射到customers表,这个文件应该和customer.class文件存放在同一个目录下。例程2-3为customer.hbm.xml文件的代码。
例程2-3 customer.hbm.xml
<?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="mypack.customer"
table="customers">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="name"
column="name" type="string"
not-null="true" />
<property name="email"
column="email" type="string"
not-null="true" />
<property name="password"
column="password" type="string"
not-null="true"/>
<property name="phone"
column="phone" type="int" />
<property name="address"
column="address" type="string" />
<property name="sex"
column="sex" type="character"/>
<property name="married"
column="is_married" type="boolean"/>
<property name="description"
column="description" type="text"/>
<property name="image"
column="image" type="binary"/>
<property name="birthday"
column="birthday" type="date"/>
<property name="registeredtime"
column="registered_time"
type="timestamp"/>
</class>
</hibernate-mapping>
2.4.1 映射文件的文档类型定义(dtd)
在例程2-3的customer.hbm.xml文件的开头声明了dtd(document type definition,文档类型定义),它对xml文件的语法和格式做了定义。hibernate的xml解析器将根据dtd来核对xml文件的语法。
每一种xml文件都有独自的dtd文件。hibernate的对象-关系映射文件使用的dtd文件的下载网址为:http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd。此外,在hibernate软件包的src/net/sf/hibernate目录下也提供了hibernate-mapping-2.0.dtd文件。在这个文件中,描述顶层元素的代码如下:
<!element hibernate-mapping (meta*,
import*, (class|subclass|joined-subclass)*,
query*,
sql-query*)>
描述顶层元素的子元素的代码如下:
<!element class (
meta*,
(cache|jcs-cache)?,
(id|composite-id),
discriminator?,
(version|timestamp)?,
(property|many-to-one|one-to-one
|component|dynamic-component|any
|map|set|list|bag|idbag|array
|primitive-array)*,
((subclass*)|(joined-subclass*))
)>
元素是对象-关系映射文件的根元素,其他元素(即以上dtd代码中括号以内的元素,如子元素)必须嵌入在元素以内。在元素中又嵌套了好多子元素。
在以上dtd代码中,还使用了一系列的特殊符号来修饰元素,表2-3描述了这些符号的作用。在创建自己的对象-关系映射文件时,如果不熟悉某种元素的语法,可以参考dtd文件。
表2-3 dtd中特殊符号的作用

根据表2-3可以看出,在元素中,、、和等子元素可以不存在,或者存在一次或者多次;在元素中,子元素必须存在且只能存在一次,元素可以不存在,或者存在一次或者多次。
此外,在映射文件中,父元素中的各种子元素的定义必须符合特定的顺序。例如,根据元素的dtd可以看出,必须先定义子元素,再定义子元素,以下映射代码颠倒了和子元素的位置:
<class name="mypack.customer"
table="customers">
<property name="name"
column="name" type="string"
not-null="true" />
<property name="email"
column="email"
type="string" not-null="true" />
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
……
</class>
hibernate的xml解析器在运行时会抛出mappingexception:
[java] 21:27:51,610 error xmlhelper:
48 - error parsing xml:
xml inputstream (24)
the content of element type "class"
must match "(meta*,(cache|jcs-cache)?,
(
id|composite-id),
discriminator?,(version|timestamp)?,
(property|many-to-one|one-to-one|component|
dynamic-component|any|map|set
|list|bag|idbag|array|primitive-array)*,
(subclass*|joined-subclass*))".
[java] net.sf.hibernate.mappingexception:
error reading resource:
mypack/customer.hbm.xml
at net.sf.hibernate.cfg.configuration.addclass
(configuration.java:357)
2.4.2 把customer持久化类映射到customers表
例程2-3的customer.hbm.xml文件用于映射customer类。如果需要映射多个持久化类,那么既可以在同一个映射文件中映射所有类,也可以为每个类创建单独的映射文件,映射文件和类同名,扩展名为"hbm.xml"。后一种做法更值得推荐,因为在团队开发中,这有利于管理和维护映射文件。
元素指定类和表的映射,它的name属性设定类名,table属性设定表名。以下代码表明和customer类对应的表为customers表:
<class name="mypack.customer"
table="customers">
如果没有设置元素的table属性,hibernate将直接以类名作为表名,也就是说,在默认情况下,与mypack.customer类对应的表为customer表。
元素包含一个子元素及多个子元素。子元素设定持久化类的oid和表的主键的映射。以下代码表明customer类的id属性和customers表中的id字段对应。
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
元素的子元素指定对象标识符生成器,它负责为oid生成惟一标识符。本书第5章(映射对象标识符)详细介绍了hibernate提供的各种对象标识符生成器的用法。
子元素设定类的属性和表的字段的映射。子元素主要包括name、type、column和not-null属性。
1.元素的name属性
元素的name属性指定持久化类的属性的名字。
2.元素的type属性
元素的type属性指定hibernate映射类型。hibernate映射类型是java类型与sql类型的桥梁。表2-4列出了customer类的属性的java类型、hibernate映射类型,以及customers表的字段的sql类型这三者之间的对应关系。
表2-4 java类型、hibernate映射类型以及sql类型之间的对应关系

从表2-4看出,如果customer类的属性为java.lang.string类型,并且与此对应的customers表的字段为varchar类型,那么应该把hibernate映射类型设为string,例如:
<property name="name"
column="name" type="string"
not-null="true" />
如果customer类的属性为java.lang.string类型,并且与此对应的customers表的字段为text类型,那么应该把hibernate映射类型设为text,例如:
<property name="description"
column="description" type="text"/>
如果customer类的属性为byte[]类型,并且与此对应的customers表的字段为blob类型,那么应该把hibernate映射类型设为binary,例如:
<property name="image" column="image"
type="binary"/>
如果没有显式设定映射类型,hibernate会运用java反射机制先识
闽公网安备 35060202000074号