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 <gavin@hibernate.org>
This commit is contained in:
parent
e9fbf23ec8
commit
e48573856e
|
@ -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)` | ✔
|
| `treat()` | Narrow an entity or embeddable type | `treat(e as Entity)` | ✔
|
||||||
| `cast()` | Narrow a basic type | `cast(x as Type)` | ✖
|
| `cast()` | Narrow a basic type | `cast(x as Type)` | ✖
|
||||||
| `str()` | Cast to a string | `str(x)` | ✖
|
| `str()` | Cast to a string | `str(x)` | ✖
|
||||||
|
| `ordinal()` | Get the ordinal value of an enum | `ordinal(x)` | ✖
|
||||||
|===
|
|===
|
||||||
|
|
||||||
Let's see what these functions do.
|
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
|
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-null]]
|
||||||
==== Functions for working with null values
|
==== Functions for working with null values
|
||||||
|
|
||||||
|
|
|
@ -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]
|
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]]
|
[[hql-functions-null]]
|
||||||
==== Functions for working with null values
|
==== Functions for working with null values
|
||||||
|
|
||||||
|
|
|
@ -76,15 +76,21 @@ public class EnumTest {
|
||||||
@Test
|
@Test
|
||||||
public void testOrdinalFunctionOnStringEnum(SessionFactoryScope scope) {
|
public void testOrdinalFunctionOnStringEnum(SessionFactoryScope scope) {
|
||||||
scope.inTransaction( session -> {
|
scope.inTransaction( session -> {
|
||||||
|
//tag::hql-ordinal-function-example[]
|
||||||
|
// enum Gender {
|
||||||
|
// MALE,
|
||||||
|
// FEMALE,
|
||||||
|
// OTHER
|
||||||
|
//}
|
||||||
List<Integer> femaleOrdinalFromString = session.createQuery(
|
List<Integer> femaleOrdinalFromString = session.createQuery(
|
||||||
"select ordinal(gender)" +
|
"select ordinal(gender)" +
|
||||||
"from EntityOfBasics e " +
|
"from EntityOfBasics e " +
|
||||||
"where e.gender = :gender",
|
"where e.gender = :gender",
|
||||||
Integer.class
|
Integer.class )
|
||||||
)
|
|
||||||
.setParameter( "gender", EntityOfBasics.Gender.FEMALE )
|
.setParameter( "gender", EntityOfBasics.Gender.FEMALE )
|
||||||
.getResultList();
|
.getResultList();
|
||||||
|
// This will return List.of(1)
|
||||||
|
//end::hql-ordinal-function-example[]
|
||||||
assertThat( femaleOrdinalFromString ).hasSize( 1 );
|
assertThat( femaleOrdinalFromString ).hasSize( 1 );
|
||||||
assertThat( femaleOrdinalFromString ).hasSameElementsAs( List.of( 1 ) );
|
assertThat( femaleOrdinalFromString ).hasSameElementsAs( List.of( 1 ) );
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Reference in New Issue