java 从数据库中检索时对休眠集合进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5081349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort hibernate collection when retrieving from database
提问by schmmd
I have an object Foo with a list of Bar. Is there a way I can set up my class so that getBars() will return a List that has been sorted with Collections.sort? In other words, I would like to run Collections.sort when the list is first populated. Presently, I call sort when I retrieve the collection, which may be redundant and is easily forgotten.
我有一个带有 Bar 列表的对象 Foo。有没有一种方法可以设置我的类,以便 getBars() 返回一个已使用 Collections.sort 排序的列表?换句话说,我想在第一次填充列表时运行 Collections.sort。目前,我在检索集合时调用 sort ,这可能是多余的并且很容易忘记。
回答by Matt Ball
Are you using J2EE-style mappings with Hibernate XML files, or are you using JPA-annotated JavaBeans?
您使用的是带有 Hibernate XML 文件的 J2EE 样式的映射,还是使用 JPA 注释的 JavaBeans?
If you're using JPA, you can use the @OrderBy
annotation to let the database sort the collection for you. You could also use @Sort
to do it on the Java side.
如果您使用 JPA,则可以使用@OrderBy
注释让数据库为您排序集合。您也可以@Sort
在 Java 端使用它。
Last, if you're writing HQL - say, in a @NamedQuery
- you can use the ORDER BY
clause.
最后,如果您正在编写 HQL - 例如,在 a @NamedQuery
- 您可以使用该ORDER BY
子句。
Lots of ways to do it!
很多方法可以做到!
回答by Sean Patrick Floyd
I think if you annotate the property with @Sort
it gets sorted automatically
我想如果你用@Sort
它来注释属性,它会自动排序
You've also got the option to specify a custom Comparator
class:
您还可以选择指定自定义Comparator
类:
@Sort(comparator=MyComparator.class, type=SortType.COMPARATOR)
Reference:
参考:
回答by hvgotcodes
check out the hibernate docs on sorted collections
查看有关排序集合的休眠文档
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-sorted.
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-sorted。
You can also put another method in your DAO that uses hql to fetch the children, which you can sort via hql.
您还可以在 DAO 中放置另一种使用 hql 获取子项的方法,您可以通过 hql 对其进行排序。
If the sorting procedure is not based off a single column, I don't think you have any choice but to do the sort yourself. You can add the logic to your domain model...
如果排序过程不是基于单个列,我认为您别无选择,只能自己进行排序。您可以将逻辑添加到域模型中...
回答by limc
If you are using XML configuration, you can set order-by
attribute:-
如果您使用 XML 配置,您可以设置order-by
属性:-
<class name="Foo" table="Foo">
...
<set name="bars" inverse="true" order-by="barName">
<key column="fooId" />
<one-to-many class="Bar" />
</set>
</class>
This way, when you invoke foo.getBars()
, you will get Bar
objects ordered by bar name, in this example.
这样,在此示例中,当您调用 时foo.getBars()
,您将获得Bar
按栏名称排序的对象。
回答by fmucar
Sorting the list when you select from the DB would be a better (let db sort it) choice instead of sorting it after selecting.
当您从数据库中选择时对列表进行排序将是一个更好的选择(让 db 对其进行排序),而不是在选择后对其进行排序。