Hibernate 集合映射

2011-12-12

Hibernate集合映射

在持久化类中,有时会使用到值类型的对象属性。所谓值类型的对象,是指它对应的类没哟偶对象标示符属性,只能作为一个持久化类的属性使用。如果持久化类中有一个值类型的集合,那么就需要一张额外的数据库表来保存这个值类型集合的数据,这张表被称为集合表。

1.     集合接口

Hibernate支持大部分重要的JDK集合接口映射,主要有以下几种。

1set>元素:可以映射类型为java.util.Set接口的属性,它的元素存放没有顺序且不允许重复;也可映射类型为java.util.SortSet接口的属性。它的元素可以按自然顺序排列。

2list>元素:可以映射类型为java.util.List接口的属性,它需要在结合属性对应的数据库表中用一个额外的索引列保存每个元素的位置。

3bag>或<idbag>元素:可以映射类型为java.util.Collection接口的属性。它的元素可重复。但不保存顺序。

4map>元素:可以映射类型为java.util.Map接口的属性,它的元素以键/值对的形式保存,也是无序的,也可以映射类型为java.util.SortMap接口的属性,它的元素可以按自然顺序排序。

4primitive-array>或<array>元素:可以映射类型为数组的属性。但它在实际运用中用得极少。

映射set

有一个学生持久化类,它可以有多项爱好(字符串类型)的选择。那么可以用如下代码清单方式定义学生类。

public class Student{

       private int id;//对象标示符

       private String name;//姓名

       private Set<String> hobbies;//个人爱好

       public Student(){}//无参构造方法

//以下省略所有属性的getter()和setter()方法

 

}

它对应的对象关系映射文件Student.hbm.xml如下:

Student.hbm.xml

<?xml version=”1.0” encoding=”UTF-8” ?>

<!DOCTYPE hibernate-mapping PUBLIC

       “-//Hibernate/Hibernate Mapping DTD 3.0//EN”

       http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<hibernate-mapping>

       <class name=”com.qiujy.domain.Student” table=”student”>

              <id name=”id” column=”id” type=”long”><generator class=”native”/></id>

              <property name=”name” not-null=”true”/>

              <!—set元素用来映射java.util.Set类型的属性

                     Name属性:指定要映射的属性名

                     Table属性:指定对应的数据库表名

n       > 

n         <set name=”hobbies” table=”student_hobby:>

u       <!--  key 子元素:指定集合属性对应的表的外键列 -- >

u       <key column=”student_id:/>

u       <! – element 子元素:映射集合内的元素 -->

u       <element type=”string” column=”hobby_name” not-null=”true”/>

</set>

</class>

</Hibernate-mapping>

这样映射之后,每个学生实例的爱好都会保存在名为student_hobby的数据库表中。

映射list

如果想记录学生所选择的爱好的顺序,可以把Student类中的hobbies属性类型改成list类型。

Public class Student{

       Private int id;//对象标示符

       Private String name;//姓名

       Pirvate List<String> hobbies;//个人爱好

       Public Student(){}/无参/构造方法

//以下省略所有属性的getter()和setter()方法

}

使用<list>元素映射时,需要把一个索引列增加到集合表中,用索引列来指定元素所在集合中的位置。

<!  -- 

              List元素用来映射java.util.List类型的属性

              Name属性:指定要映射的属性名

              Table属性:指定对应的数据库表名

n       > 

<list name=”hobbies” table=”student_hobby”>

       <! --  key 元素:指定集合属性对应的表的外键列 -- >

       <key column=”student_id”/>

       <!—list-index 元素:指定索引列 -- >

       <list-index column=”position”/>

       <!—element 元素:映射集合内的元素 -- >

       <element type=”string” column=”hobby_name” not-null=”ture”>

</list>

映射bag

允许重复元素的无序集合被称为包(包),但在java集合框架中并没有一个对应语义的实现类。可以使用java.util.Collection接口或java.util.List接口来表示这种语义,具体的实现由Hibernate框架内部区处理。

把Student类中的hobbies属性类型改为Connection

Public calss Student{

       Private int id ;//对象标示符

       Private String name;//姓名

       Private Collection<String> hobbies;//个人爱好

       Public Student({}

}

对应的映射文件Student.hbm.xml

<!--  

Bag元素用来映射java.util.Collection或java.util.List类型的属性

       Name属性:指定要映射的属性名

       Table属性:指定对应的数据库表名

n       > 

<bag name=”hobbies” table=”student_hobby”>

       <! -- key 元素:指定集合属性对应的表的外键列 -- >

       <key column=”student_id”/>

       <!—element 元素:映射集合内的元素 --  >

       <element type=”string” column=”hobby_name” not-null=”true”/>

</bag>

映射map

下面是使用键/值对的方式

Public calss Student{

       Private int id;//对象标示符

       Private String name;//姓名

       Private    Map<Long,String> hobbies;//个人爱好

       Public Studetn(){}//无参构造方法

//以下省略所有属性的getter()和setter()方法….

 

}

对应的映射文件 Student.hbm.xml

<! -- 

       Bag元素用来映射java.util.Collection或java.util.List类型的属性

              Name属性:指定属性的属性名

              Table属性:指定对应的数据库表名

n       > 

<map name=”hobbies” table=”student_hobby”>

       <!--  key 元素:指定集合属性对应的表的外键列 --  >

       <key column=”student_id”/>

       <!-- map-key 元素:指定map属性的键在对应表中的列 --  >

       <map-key column=”hobby_id” type=”long”/>

       <!--  element元素:映射集合内的元素-- >

       <element type=”string” column=”hobby_name” not-null=”true”/>

</map>

排序集合和有序集合

在实际应用中,经常需要集合中的元素师按一定要求排列好顺序后再返回给使用者的,这种需求在Hibernate中可能使用排序集合和有序集合来完成。

排序集合是指数据库中获取到集合中时,使用一个java比较器在内存中进行后存放

Public class Student {

       Private int id;//对象标示符

       Private String name;//姓名

       Private SortedSet<String> hobbies//个人爱好

       Public Student(){}//无参构造方法

//以下为所有省略getter()和setter()方法….

}

对应映射文件Student.hbm.xml

<! -- 

       Set元素用来映射java.util.Set类型的属性

              Name属性:指定要映射的属性名

              Table属性:指定对应的映射的数据库表名

              Sort属性:指定排序的规则。可选值:unsorted(不排序)、natural(按compareTo()方法进行比较厚排序)、自定义的排序器名

n       > 

n       <set name=”hobbies” table=”student_hobby” sort=”natural”>

n       <!--  key 元素:指定集合属性对应的表的外键列 -- >

n       <key column=”student_id”/>

n       <!—element 子元素:映射集合内的元素 --  >

n       <elemnt type=”string” column=”hobby_name” not-null=”true”/>

</set>

在<set>元素中通过指定sort=”natural”,告诉Hibernate使用SortSet,并使用自然排序来对集合内的元素进行排序存放。

       有序集合是指在数据廤获取数据时就使用SQL语句的order by语句排好序,然后再封装到结合中存放。这种情况下持久化类中的集合属性仍旧使用Set或Map.但对应的映射文件中的<set>或<map>元素需要添加order-by属性来指定SQL中order by子句。映射文件片段如下:

<!--  

       Set元素用来映射java.util.Set类型的属性

              Name属性:指定要映射的属性名

              Table属性:指定对应的数据库表名

              Order-by属性:指定SQL语句中的order-by子句。这个子句中出现的名要是对应数据库表的列名

n       > 

n       <set name=”hobbies” table=”student_hobby” order_by=”hobby-name desc”>

n       <!—key 子元素:指定集合属性对应的表的外键列-- >

n       <key column=”student-id”/>

n       <!--  element 子元素:映射集合内的元素 --  >

n       <element type=”string” column=”hobby_name” not-null=”true”/>

</set>

 

             

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值