Package org.hibernate.annotations
Annotation Type SQLOrder
-
@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface SQLOrder
Order a collection using an expression or list of expression written in native SQL. For example,@SQLOrder("first_name, last_name")
,@SQLOrder("char_length(name) desc")
, or even@SQLOrder("name asc nulls last")
.The order is applied by the database when the collection is fetched, but is not maintained by operations that mutate the collection in memory.
If the collection is a
Set
orMap
, the order is maintained using aLinkedHashSet
orLinkedHashMap
. If the collection is a bag orList
, the order is maintained by the underlyingArrayList
.There are several other ways to order or sort a collection:
- Use the JPA-defined
OrderBy
annotation to order using an expression written in HQL/JPQL. Since HQL is more portable between databases, this is the preferred alternative most of the time. - Use
SortComparator
to sort the collection in memory using aComparator
, orSortNatural
to sort the collection in memory according to its natural order. - Use
OrderColumn
to maintain the order of aList
with a dedicated index column.
It's illegal to use
SQLOrder
together with the JPA-definedOrderBy
for the same collection.- Since:
- 6.3
- See Also:
OrderBy
,SortComparator
,SortNatural
,DialectOverride.SQLOrder
- Use the JPA-defined
-
-
Element Detail
-
value
String value
A comma-separated list native SQL expressions used to sort the collection elements. Each element of the list may optionally specify:asc
-ending ordesc
-ending order, or evennulls first
ornulls last
.
-
-