Hibernate ordering in sql

核能气质少年 提交于 2019-12-10 00:25:18

问题


Is it possible to sort the Set collection using hibernate, in sql without using SortedSet interface, without using @OrderBy annotation - just using criteria's addOrder.

I've tried it and it adds order by clause but the set isn't sorted.

I use hibernate 3.4.


回答1:


NHibernate Sets don't have "order" even though the set mapping supports an order-by. See Ayende's explanation:

Note that [order-by] does not work with generic sets and that in general, you don’t want to rely on those ordering properties, you want to use the natural properties of the selected collection. Sets are, by definition, unordered set of unique elements.

You can append the order by clause to the underlying SQL, but that order is lost when NHibernate builds the set. Using an "ordered" set or other collection type (bag or list) will work, of course.




回答2:


We will work correctly if you change the "Set" to "List" and methods that do not exist in the "List" put the equivalent.

Regards,



来源:https://stackoverflow.com/questions/12216031/hibernate-ordering-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!