0

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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I know. I added the question also with nhibernate, despite that in fact I am using java, because I am a bit desperate with this subject. When I annotate the Set with @OrderBy annotation then I'm getting sorted set (I suspect hibernate use some sorted implementation then), but... if I didn't annotate the field and only add ordering in criteria then returned Set is not sorted. In both cases generated sql queries are the same. So the conclusion is that using OrderBy annotation forces hibernate to use SortedSet implementation, but I don't want to use this annotation by default.
0

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

Regards,

1 Comment

Because of some other reasons I need a Set there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.