bernate的集合映射与sort、order-by属性
来源:互联网 发布:初级程序员教程 编辑:程序博客网 时间:2024/06/16 12:25
<div class="postBody"> <div id="cnblogs_post_body"><p>【Hibernate】Hibernate的集合映射与sort、order-by属性</p>
常用集合Set、List、Map,相信大家都很熟悉,面试中也会经常问。Set和List都继承了Collection接口,Set是无序不可重复的,不可以存储相同的元素;而Lsit是顺序存储的,可存储重复元素。Map不是继承Collection的,Map提供key到value的映射,Map也是不可重复的(key不可重复),一个Map中不能包含相同的key,每个key只能映射一个value。
持久化类的属性是集合时,要把它们存到数据库中,需要增设数据库表来完成映射工作。例如一个学生的教育背景可能不止一个,这个时候持久化类的属性是集合,这个时候数据库就需要再提供一张数据库表来存储。
由上图可以看出,集合的映射应该怎么写了。集合的映射大致一样,我们用Set的映射来详细说明。
<set name="education" table="student_education" order-by=" education DESC"> <key column="student_id"></key> <element type="string" column="education"></element></set>
name属性是指对象的集合属性名,table属性是指集合表(数据库表)的名称,key子元素是指集合外键的列名,element子元素用来存放集合元素的列的信息,order-by属性是指查询数据库时指定order by 子句,这是在数据库中的排序。对于Set的排序,还有另外一个属性sort,它是在内存中排序的,默认是unsorted,sort还有其它两个值:natural和comparatorClass,当使用sort属性时,要求使用的是可以排序的Set,例如TreeSet等。
List的映射:
<list name="education" table="student_education"> <key column="student_id"></key> <list-index column="list_id"></list-index> <element type="string" column="education"></element></list>
List的映射与Set很相似,多了一个list-index子元素,是指定List的索引对应于集合表中的哪个字段。因为List是有序的,可以通过索引来取值。也正因为有序,所以不可以使用sort属性进行排序。
Map的映射:
<p><map name="education" table="student_education"></p><p> <keycolumn="student_id"></key></p><p> <map-keytype="string" column="map_key"></map-key></p><p> <element type="string"column="education"></element></p><p></map></p>
map-key子元素是指定Map的key值对应集合表中的哪个字段。
除了这3个常用的集合的映射,hibernate还提供了另外两种集合的映射:数组和Bag。数组性质跟List相似,但是数组的长度不可变;Bag也与List相似,但是它是无序可重复的。因此数组的映射与List基本一样:
<array name="education" table="student_education"> <key column="student_id"></key> <list-index column="list_id"></list-index> <element type="string" column="education"></element></array>
而Bag的映射就比List少一个list-index子元素:
<bag name="education" table="student_education"> <keycolumn="student_id"></key> <elementtype="string" column="education"></element></bag>
注意:
使用集合属性时,一定要使用接口,而不能声明为具体实现类。因为经过session操作后,集合就变成hibernate自己的集合实现类。
最好再整理一次关于sort也order-by属性的使用:
由于Set与Map都是无序的,所以我们可以使用sort或者order-by属性对它们进行排序。sort是在内存中排序的,要求使用的Set与Map也是可排序的。这种排序方法不建议使用,使用较多的,排序也比较快的是使用order-by属性,直接在数据库中排序。
- bernate的集合映射与sort、order-by属性
- 【Hibernate】Hibernate的集合映射与sort、order-by属性
- 【Hibernate】Hibernate的集合映射与sort、order-by属性
- Hibernate映射集合属性4__排序用的order-by属性
- Order By与Sort
- hive 中order by 与 sort by 的区别
- Hibernate映射set集合,order-by的作用
- hive sql order by 与sort by
- d3选择集合的操作方法(四):sort与order
- hive中order by 和sort by与distribute by
- Hive中SELECT TOP N的方法(order by与sort by的区别)
- Hive中SELECT TOP N的方法(order by与sort by)
- Hive中SELECT TOP N的方法(order by与sort by)
- bernate之中inverse与cascade的异同
- hive top n (order by与sort by区别)
- SORT ORDER BY STOPKEY
- SORT ORDER BY STOPKEY
- hive sort by|order by
- bzoj 1066 最大流
- 张高兴的 Windows 10 IoT 开发笔记:HC-SR04 超声波测距模块
- 广度优先搜索——马的遍历
- ffmpeg编译保留代码不做优化
- 求三角形面积 (sdut oj)
- bernate的集合映射与sort、order-by属性
- 微信LazyMan笔试题的深入解析和实现
- Windows下sqlmap安装方法之配置Windows下的环境变量
- POJ 1426 Find The Multiple (BFS)
- 求实数绝对值 (sdut oj)
- C/C++程序训练6---歌德巴赫猜想的证明 (sdut oj)
- 微信LazyMan笔试题的深入解析和实现_0
- Poj 1201
- Hrbust1494网络 tarjan求割点