Skip to content

Commit e485738

Browse files
lucamoltenigavinking
authored andcommitted
HHH-16861 documentation for HQL ordinal() function
Added `ordinal` to QueryLanguage.adoc Added `ordinal` to Expressions.adoc Update documentation/src/main/asciidoc/querylanguage/Expressions.adoc Co-authored-by: Gavin King <[email protected]>
1 parent e9fbf23 commit e485738

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

documentation/src/main/asciidoc/querylanguage/Expressions.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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

552553
Let's see what these functions do.
@@ -616,6 +617,18 @@ The function `str(x)` is a synonym for `cast(x as String)`.
616617
select 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

documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,23 @@ The function `str(x)` is a synonym for `cast(x as String)`.
820820
----
821821
include::{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

hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff 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
} );

0 commit comments

Comments
 (0)