File tree Expand file tree Collapse file tree 3 files changed +41
-6
lines changed
documentation/src/main/asciidoc
userguide/chapters/query/hql
hibernate-core/src/test/java/org/hibernate/orm/test/hql Expand file tree Collapse file tree 3 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -547,6 +547,7 @@ The following special functions make it possible to discover or narrow expressio
547547| `treat()` | Narrow an entity or embeddable type | `treat(e as Entity)` | ✔
548548| `cast()` | Narrow a basic type | `cast(x as Type)` | ✖
549549| `str()` | Cast to a string | `str(x)` | ✖
550+ | `ordinal()` | Get the ordinal value of an enum | `ordinal(x)` | ✖
550551|===
551552
552553Let's see what these functions do.
@@ -616,6 +617,18 @@ The function `str(x)` is a synonym for `cast(x as String)`.
616617select str(id) from Order
617618----
618619
620+ [[function-ordinal]]
621+ [discrete]
622+ ===== Extracting the ordinal value of an enum
623+
624+ The function `ordinal(x)` extracts the ordinal value of an enum.
625+ It supports both enum fields mapped as `ORDINAL` and `STRING`.
626+
627+ [source, hql]
628+ ----
629+ select ordinal(p.type) from Phone p
630+ ----
631+
619632[[functions-null]]
620633==== Functions for working with null values
621634
Original file line number Diff line number Diff line change @@ -820,7 +820,23 @@ The function `str(x)` is a synonym for `cast(x as String)`.
820820----
821821include::{example-dir-hql}/HQLTest.java[tags=hql-str-function-example]
822822----
823+
824+ ====
825+
826+ [[hql-function-ordinal]]
827+ ===== `ordinal()`
828+
829+ The function `ordinal(x)` extracts the ordinal value of an enum.
830+ It supports both enum fields mapped as `ORDINAL` and `STRING`.
831+
823832====
833+ [source, java, indent=0]
834+ ----
835+ include::{example-dir-hql}/EnumTest.java[tags=hql-ordinal-function-example]
836+ ----
837+
838+ ====
839+
824840
825841[[hql-functions-null]]
826842==== Functions for working with null values
Original file line number Diff line number Diff line change @@ -76,15 +76,21 @@ public void testOrdinalFunctionOnOrdinalEnum(SessionFactoryScope scope) {
7676 @ Test
7777 public void testOrdinalFunctionOnStringEnum (SessionFactoryScope scope ) {
7878 scope .inTransaction ( session -> {
79+ //tag::hql-ordinal-function-example[]
80+ // enum Gender {
81+ // MALE,
82+ // FEMALE,
83+ // OTHER
84+ //}
7985 List <Integer > femaleOrdinalFromString = session .createQuery (
80- "select ordinal(gender)" +
81- "from EntityOfBasics e " +
82- "where e.gender = :gender" ,
83- Integer .class
84- )
86+ "select ordinal(gender)" +
87+ "from EntityOfBasics e " +
88+ "where e.gender = :gender" ,
89+ Integer .class )
8590 .setParameter ( "gender" , EntityOfBasics .Gender .FEMALE )
8691 .getResultList ();
87-
92+ // This will return List.of(1)
93+ //end::hql-ordinal-function-example[]
8894 assertThat ( femaleOrdinalFromString ).hasSize ( 1 );
8995 assertThat ( femaleOrdinalFromString ).hasSameElementsAs ( List .of ( 1 ) );
9096 } );
You can’t perform that action at this time.
0 commit comments