休眠:订购一个集合 [英] Hibernate: ordering a Set

查看:106
本文介绍了休眠:订购一个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有一套书籍的班级人员。在特定的情况下,订购或排序的集合是没有意义的。



现在说我有一个搜索页面,里面有一个表格,显示Person和Book的连接。我希望能够根据Person和Book的字段对结果进行排序,然后从Hibernate中获取一个List,并对它进行迭代。



因为集合是一个设置时,书籍的排序已经消失(Hibernate的PersistentSet封装了一个HashSet书籍,它没有排序)。

所以,使用这种方法我也不能得到结果按照Book字段排序。



如果我将集合从Set更改为List,那么我的模型在语义上不正确。在模型中保留订单没有任何意义。



是否有一种方法可以保持图书的排序?也许有一种方法让PersistentSet包装一个LinkedHashSet(它是有序的),其中的顺序是由我的搜索标准定义的?



干杯!

$ hibernate支持将集合映射为 SortedSet 。在你的映射中,你基本上只需要指定一个 order-by 子句。请参阅本章中的手动


I have a class Person who has a set of Books. It is not meaningful in the particular case to have an ordered or sorted collection.

Say now that I have a search page with a table showing the join of Person and Book. I want to be able to sort the results by fields from both Person AND Book, and then get a List from Hibernate, and iterate over it.

Because the collection is a Set, the ordering of the Books has vanished (PersistentSet of Hibernate wraps a HashSet of Books, which is not ordered).

So, with this approach I cannot have results also ordered by Book fields.

If I change the collection from Set to List, my model is semantically incorrect. There is no meaning for keeping order in the model.

Is there an approach to keep the ordering of Books? Perhaps there is a way for the PersistentSet to wrap a LinkedHashSet (which is ordered), where the order is defined by my search Criteria?

Cheers!

解决方案

Hibernate supports mapping a collection as a SortedSet. In your mappings you basically just need to specify an order-by clause. Take a look at this chapter in the reference manual.

这篇关于休眠:订购一个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆