diff --git a/documentation/src/main/asciidoc/querylanguage/Expressions.adoc b/documentation/src/main/asciidoc/querylanguage/Expressions.adoc index 9c34035a4d..509c58c794 100644 --- a/documentation/src/main/asciidoc/querylanguage/Expressions.adoc +++ b/documentation/src/main/asciidoc/querylanguage/Expressions.adoc @@ -547,6 +547,7 @@ The following special functions make it possible to discover or narrow expressio | `treat()` | Narrow an entity or embeddable type | `treat(e as Entity)` | ✔ | `cast()` | Narrow a basic type | `cast(x as Type)` | ✖ | `str()` | Cast to a string | `str(x)` | ✖ +| `ordinal()` | Get the ordinal value of an enum | `ordinal(x)` | ✖ |=== Let's see what these functions do. @@ -616,6 +617,18 @@ The function `str(x)` is a synonym for `cast(x as String)`. select str(id) from Order ---- +[[function-ordinal]] +[discrete] +===== Extracting the ordinal value of an enum + +The function `ordinal(x)` extracts the ordinal value of an enum. +It supports both enum fields mapped as `ORDINAL` and `STRING`. + +[source, hql] +---- +select ordinal(p.type) from Phone p +---- + [[functions-null]] ==== Functions for working with null values diff --git a/documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc b/documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc index b9689d0548..ddffb6e0cc 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc @@ -820,8 +820,24 @@ The function `str(x)` is a synonym for `cast(x as String)`. ---- include::{example-dir-hql}/HQLTest.java[tags=hql-str-function-example] ---- + ==== +[[hql-function-ordinal]] +===== `ordinal()` + +The function `ordinal(x)` extracts the ordinal value of an enum. +It supports both enum fields mapped as `ORDINAL` and `STRING`. + +==== +[source, java, indent=0] +---- +include::{example-dir-hql}/EnumTest.java[tags=hql-ordinal-function-example] +---- + +==== + + [[hql-functions-null]] ==== Functions for working with null values diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java index be69ee55c1..8ce7523d90 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java @@ -76,15 +76,21 @@ public void testOrdinalFunctionOnOrdinalEnum(SessionFactoryScope scope) { @Test public void testOrdinalFunctionOnStringEnum(SessionFactoryScope scope) { scope.inTransaction( session -> { + //tag::hql-ordinal-function-example[] + // enum Gender { + // MALE, + // FEMALE, + // OTHER + //} List femaleOrdinalFromString = session.createQuery( - "select ordinal(gender)" + - "from EntityOfBasics e " + - "where e.gender = :gender", - Integer.class - ) + "select ordinal(gender)" + + "from EntityOfBasics e " + + "where e.gender = :gender", + Integer.class ) .setParameter( "gender", EntityOfBasics.Gender.FEMALE ) .getResultList(); - + // This will return List.of(1) + //end::hql-ordinal-function-example[] assertThat( femaleOrdinalFromString ).hasSize( 1 ); assertThat( femaleOrdinalFromString ).hasSameElementsAs( List.of( 1 ) ); } );