2.5 组件映射
Component是一个被包含在其他实体中的对象。组件可能和集合映射一起使用。
2.5.1 简单组件
只需提供必要的属性。示例如下:
package com.weportal.component;


public class Country ...{
private long id;
private String name;
private long population;
private double area;
private Position position;

/** *//**
* @return Returns the area.
*/

public double getArea() ...{
return area;
}

/** *//**
* @param area The area to set.
*/

public void setArea(long area) ...{
this.area = area;
}

/** *//**
* @return Returns the id.
*/

public long getId() ...{
return id;
}

/** *//**
* @param id The id to set.
*/

public void setId(long id) ...{
this.id = id;
}

/** *//**
* @return Returns the name.
*/

public String getName() ...{
return name;
}

/** *//**
* @param name The name to set.
*/

public void setName(String name) ...{
this.name = name;
}

public long getPopulation() ...{
return population;
}

public Position getPosition() ...{
return position;
}

public void setPopulation(long population) ...{
this.population = population;
}

public void setPosition(Position position) ...{
this.position = position;
}
}
package com.weportal.component;


public class Position ...{
private String continent;
private String direction;

/** *//**
* @return Returns the continent.
*/

public String getContinent() ...{
return continent;
}

/** *//**
* @param continent The continent to set.
*/

public void setContinent(String continent) ...{
this.continent = continent;
}

/** *//**
* @return Returns the direction.
*/

public String getDirection() ...{
return direction;
}

/** *//**
* @param direction The direction to set.
*/

public void setDirection(String direction) ...{
this.direction = direction;
}
}
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.weportal.component">
<class name="Country">
<id name="id">
<generator class="hilo"/>
</id>
<property name="name" column="NAME" />
<component name="position">
<property name="continent"></property>
<property name="direction"></property>
</component>
</class>
</hibernate-mapping>
country表的定义如下:

create table `hibernate`.`country`(
`id` bigint default '' not null,
`NAME` varchar(255),
`continent` varchar(255),
`direction` varchar(255),
primary key (`id`)
);

create unique index `PRIMARY` on `hibernate`.`country`(`id`);
2.5.2 组件中的一对一
组件可能引用了其他的持久化类,所以在组件中可以包含一对一的关系。
2.5.3 组件中的多对一
在组件中添加对其他实体的引用。
2.5.4 组件中的集合
2.5.5 动态组件
可以利用动态模拟的组件来代替固定的组件元素,使用dynamic-component元素来声明动态组件,设计动态组件时不需要提供实际的组件类,组件的属性在运行时可以灵活掌握。
2.6 动态类
dynamic-class元素可以用来描述动态类。
2.6.1简单动态类
与动态的组件相似,都是利用Map来模拟类的属性,只不过动态类模拟的是实体,动态组件被其他实体引用。
2.6.2 动态类的一对一
2.6.3 动态类的多对一
2.6.4 动态类的组件
2.6.5 动态类的动态组件
2.6.7 动态类的集合
2.6.8 动态类的子元素和属性
2.7 类型映射
数据类型不能独立于引用它的实体,称作值。
2.7.1 基本值类型
基本类型可以大致的分为:integer, long, short, float, double, character, byte, boolean, yes_no, true_false。这些类型都对应Java 的原始类型或者其包装类,来适合(特定厂商的)SQL 字段类型。boolean, yes_no 和true_false 都是Java 中boolean 或者java.lang.Boolean 的另外说法。
string | 从java.lang.String 到 VARCHAR (或者 Oracle 的 VARCHAR2)的映射。 |
date, time, timestamp | 从java.util.Date 和其子类到SQL 类型DATE, TIME 和TIMESTAMP (或等价类型)的映射。 |
calendar, calendar_date | 从java.util.Calendar 到SQL 类型TIMESTAMP 和 DATE(或等价类型)的映射。 |
text | 把长Java 字符串映射为SQL 的CLOB 或者TEXT 类型。 |
big_decimal | 从java.math.BigDecimal 到 NUMERIC (或者 Oracle 的NUMBER 类型)的映射。 |
locale, timezone, currency | 从java.util.Locale, java.util.TimeZone 和java.util.Currency 到VARCHAR(或者 Oracle 的VARCHAR2 类型)的映射. Locale 和 Currency 的实例被映射为它们的ISO 代码。TimeZone 的实例被影射为它的ID。 |
class | 从java.lang.Class 到 VARCHAR (或者 Oracle 的VARCHAR2 类型)的映射。Class 被映射为它的全限定名。 |
binary
| 把字节数组(byte arrays)映射为对应的 SQL 二进制类型。 |
serializable | 把可序列化的Java 类型映射到对应的SQL 二进制类型。你也可以为一个并非默认为基本类型或者实现PersistentEnum 接口的可序列化Java 类或者接口指定Hibernate 类型serializable。 |
clob, blob | JDBC 类 java.sql.Clob 和 java.sql.Blob 的映射。某些程序可能不适合使用这个类型,因为blob 和clob 对象可能在一个事务之外是无法重用的。(而且, 驱动程序对这种类型的支持充满着补丁和前后矛盾。)实体及其集合的唯一标识可以是任何基础类型,除了binary、 blob 和 clob 之外。 |
在net.sf.hibernate.Hibernate 中,定义了基础类型对应的Type 常量。比如,Hibernate.STRING 代表string 类型。
2.7.2 枚举类型
hibernate3.0是基于java5.0的,所以开发枚举类型时直接使用enum即可。例如:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.weportal.cloth">
<class name="Cloth">
<id name="id" type="long">
<generator class="hilo" />
</id>
<property name="name" />
<property name="size" type="com.weportal.cloth.Size">
</property>
</class>
</hibernate-mapping>

生成的数据库表定义为:

create table `hibernate`.`cloth`(
`id` bigint default '' not null,
`name` varchar(255),
`size` tinyblob,
primary key (`id`)
);

create unique index `PRIMARY` on `hibernate`.`cloth`(`id`);
2.7.3 自定义数据类型
根据需要开发自定义的数据类型,需要实现接口com.hibernate.usertype.CompositeUserType或者org.hibernate.usertype.UserType接口。