Hibernate值类型的对象没有OID,它们的生命周期依赖于集合所属对象的生命周期。
本文将介绍Hibernate值类型集合的映射,包括<set>、<idbag>、<list>和<map>,以及它们排序的方法、组件类型值集合的映射。
1.映射Set(集)
◆JAVA代码
1 | private Set images= new HashSet(); |
2 | public Set getImages() { |
5 | public void setImages(Set images) { |
◆表结构关系

◆映射文件配置
1 | < set name = "images" table = "IMAGES" lazy = "true" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
2.映射Bag(包)
Bag集合中的对象不按特定方式排序,但是允许有重复对象。在Java集合API中并没有提供Bag接口,Hibernate允许在持久化类中用List来模拟Bag的行为。
◆JAVA代码
1 | private List images= new ArrayList(); |
2 | public List getImages() { |
5 | public void setImages(List images) { |
◆表结构关系

◆映射文件配置
1 | < idbag name = "images" table = "IMAGES" lazy = "true" > |
2 |
< collection-id type = "long" column = "ID" > |
3 |
< generator class = "increment" /> |
5 |
< key column = "CUSTOMER_ID" /> |
6 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
<idbag>元素与<set>元素的配置很相似,区别在于<idbag>中增加了<collection-id>子元素,它用于设置IMAGES表的ID主键。
3.映射List(列表)
尽管Customer类的images属性被定义为List类型,但是由于在Customer.hbm.xml文件中用元素来映射它,因此images集合中的元素并不会按照索引位置排序。如果希望images集合中允许存放重复元素,并且按照索引位置排序,首先应该在IMAGES表中定义一个POSITION字段,代表每个元素在集合中的索引位置。CUSTOMER_ID和POSITION字段共同构成了IMAGES表的主键。
◆JAVA代码
同前文<idbag>中一样。
◆表结构关系

◆映射文件配置
1 | < list name = "images" table = "IMAGES" lazy = "true" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< list-index column = "POSITION" /> |
4 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
4.映射Map
◆JAVA代码
1 | private Map images=new HashMap(); |
2 | public Map getImages() { |
5 | public void setImages(Map images) { |
◆表结构关系

◆映射文件配置
1 | < map name = "images" table = "IMAGES" lazy = "true" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< map-key column = "IMAGE_NAME" type = "string" /> |
4 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
5.对集合排序
Hibernate对集合中的元素支持两种排序方式:
●在数据库中排序:简称为数据库排序,当Hibernate通过select语句到数据库中检索集合对象时,利用order by子句进行排序。
●在内存中排序:简称为内存排序,当Hibernate把数据库中的集合数据加载到内存中的Java集合中后,利用Java集合的排序功能进行排序,可以选择自然排序或者客户化排序两种方式。
在映射文件中,Hibernate用sort属性来设置内存排序,用order-by属性来设置数据库排序,表14-2显示了<set>、<idbag>、<list>和<map>元素的排序属性。
下表为 <set>、<idbag>、<list>和<map>元素的排序属性
排 序 属 性 | <set> | <idbag> | <list> | <map> |
sort属性(内存排序) | 支持 | 不支持 | 不支持 | 支持 |
order-by属性(数据库排序) | 支持 | 支持 | 不支持 | 支持 |
从上表看出,<set>和<map>元素支持内存排序和数据库排序,<list>元素不支持任何排序方式,而<idbag>仅支持数据库排序。
◆在数据库中对集合排序
1 | < set name = "images" table = "IMAGES" lazy = "true" order-by = "FILENAME asc" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
1 | < set name = "images" table = "IMAGES" lazy = "true" order-by = "lower(FILENAME) desc" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
◆在内存中对集合排序
1 | < set name = "images" table = "IMAGES" lazy = "true" sort = "natural" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
1 | < set name = "images" table = "IMAGES" lazy = "true" sort = "mypack.ReverseStringComparator" > |
2 |
< key column = "CUSTOMER_ID" /> |
3 |
< element column = "FILENAME" type = "string" not-null = "true" /> |
上文第2个内存排序的例子中,sort属性配置的是一个实现了java.util.SortedSet接口的实现类。若是<map>的话,则是java.util.SortedMap的实现类。
6.映射组件类型集合
如果客户的照片包含照片名、文件名、长和宽等信息,可以专门定义一个Image组件类来表示照片。下图显示了Customer类和Image类的组成关系。

从上图可以看出,Customer类与Image类之间是一对多的组成关系。因此,可以在Customer类中定义一个Set/List/Map类型的images集合来存放所有的Image对象。
Image类作为一种值类型,没有OID,此外,由于Image对象会存放在Java集合中,为了保证Java集合正常工作,应该在Image类中实现equals()和hashCode()方法。
◆Set组件类型集合

01 | < set name = "images" table = "IMAGES" lazy = "true" order-by = "IMAGE_NAME asc" > |
02 |
< key column = "CUSTOMER_ID" /> |
03 |
< composite-element class = "mypack.Image" > |
04 |
< parent name = "customer" /> |
05 |
< property name = "name" column = "IMAGE_NAME" not-null = "true" /> |
06 |
< property name = "filename" column = "FILENAME" not-null = "true" /> |
07 |
< property name = "sizeX" column = "SIZEX" not-null = "true" /> |
08 |
< property name = "sizeY" column = "SIZEY" not-null = "true" /> |
◆idbag组件类型集合

01 | < idbag name = "images" table = "IMAGES" lazy = "true" order-by = "IMAGE_NAME asc" > |
02 |
< collection-id type = "long" column = "ID" > |
03 |
< generator class = "increment" /> |
05 |
< key column = "CUSTOMER_ID" /> |
06 |
< composite-element class = "mypack.Image" > |
07 |
< parent name = "customer" /> |
08 |
< property name = "name" column = "IMAGE_NAME" /> |
09 |
< property name = "filename" column = "FILENAME" not-null = "true" /> |
10 |
< property name = "sizeX" column = "SIZEX" /> |
11 |
< property name = "sizeY" column = "SIZEY" /> |
◆map组件类型集合

view source
01 | < map name = "images" table = "IMAGES" lazy = "true" order-by = "IMAGE_NAME asc" > |
02 |
< key column = "CUSTOMER_ID" /> |
03 |
< map-key type = "string" column = "IMAGE_NAME" /> |
04 |
< composite-element class = "mypack.Image" > |
05 |
< parent name = "customer" /> |
06 |
< property name = "filename" column = "FILENAME" not-null = "true" /> |
07 |
< property name = "sizeX" column = "SIZEX" /> |
08 |
< property name = "sizeY" column = "SIZEY" /> |
转载地址:http://www.coder.xxx/archives/57.html