document new collection aggregate function syntax

and fix some other problems I found in the examples
This commit is contained in:
Gavin King 2022-01-10 13:39:29 +01:00
parent 3e8f1c67df
commit e6128551b3
2 changed files with 52 additions and 52 deletions

View File

@ -724,6 +724,18 @@ include::{sourcedir}/HQLTest.java[tags=hql-coalesce-example]
TIP: HQL allows `ifnull()` as a synonym for `coalesce()` in the case of exactly two arguments.
===== `size()`
The number of elements of a collection or to-many association.
[[hql-size-example]]
====
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-size-example]
----
====
===== `concat()`
Produces a string by concatenating its arguments.
@ -985,13 +997,16 @@ include::{sourcedir}/HQLTest.java[tags=hql-collection-index-operator-example]
There are even more functions which accept a collection-valued attribute or to-many association:
|===
| HQL Function | Applies to | Purpose
| HQL Function (modern syntax) | HQL Function (old syntax) | Applies to | Purpose
| `size()` | Any collection | The size of the collection.
| `maxelement()` | Collections of basic type | The maximum element as determined by applying the `max()` SQL aggregation.
| `minelement()` | Collections of basic type | The minimum element as determined by applying the `min()` SQL aggregation.
| `maxindex()` | Indexed collections (lists and maps) | The maximum index (key/position) as determined by applying the `max()` SQL aggregation.
| `minindex()` | Indexed collections (lists and maps) | The minimum index (key/position) as determined by applying the `min()` SQL aggregation.
| `max(element x)`| `maxelement(x)` | Collections of basic type | The maximum element
| `min(element x)`| `minelement(x)` | Collections of basic type | The minimum element
| `sum(element x)`| — | Collections of basic type | The sum of the elements
| `avg(element x)`| — | Collections of basic type | The sum of the elements
| `max(index x)`| `maxindex(x)` | Indexed collections (lists and maps) | The maximum list index or map key
| `min(index x)`| `minindex(x)` | Indexed collections (lists and maps) | The minimum list index or map key
| `sum(index x)`| — | Indexed collections (lists and maps) | The sum of the list indexes or map keys
| `avg(index x)`| — | Indexed collections (lists and maps) | The average of the list indexes or map keys
|===
We've intentionally left two functions off this list, so we can come back to them <<hql-elements-indices,later>>.

View File

@ -1331,7 +1331,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
// select clause date/time arithmetic operations
Integer years = entityManager.createQuery(
"select year(current_date()) - year(p.createdOn) " +
"select year(local date) - year(p.createdOn) " +
"from Person p " +
"where p.id = 1L",
Integer.class)
@ -1350,7 +1350,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where year(current_date()) - year(p.createdOn) > 1",
"where year(local date) - year(p.createdOn) > 1",
Person.class)
.getResultList();
//end::hql-numeric-arithmetic-example[]
@ -1625,15 +1625,17 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect(SQLServerDialect.class)
public void test_hql_current_date_function_example_sql_server() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::hql-extract-function-example[]
List<Call> calls = entityManager.createQuery(
"select c " +
"from Call c " +
"where c.timestamp = current_date()",
"where extract(date from c.timestamp) = local date",
Call.class)
.getResultList();
//end::hql-extract-function-example[]
assertEquals(0, calls.size());
});
}
@ -1720,7 +1722,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::hql-extract-function-example[]
List<Integer> years = entityManager.createQuery(
"select extract(YEAR from c.timestamp) " +
"select extract(year from c.timestamp) " +
"from Call c ",
Integer.class)
.getResultList();
@ -1825,26 +1827,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
});
}
@Test
public void test_hql_collection_expressions_example_4() {
doInJPA(this::entityManagerFactory, entityManager -> {
Call call = entityManager.createQuery("select c from Call c", Call.class).getResultList().get(0);
Phone phone = call.getPhone();
//tag::hql-collection-expressions-example[]
// the above query can be re-written with member of
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where :phone member of p.phones",
Person.class)
.setParameter("phone", phone)
.getResultList();
//end::hql-collection-expressions-example[]
assertEquals(1, persons.size());
});
}
@Test
public void test_hql_collection_expressions_example_5() {
doInJPA(this::entityManagerFactory, entityManager -> {
@ -1864,6 +1846,26 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
});
}
@Test
public void test_hql_collection_expressions_example_4() {
doInJPA(this::entityManagerFactory, entityManager -> {
Call call = entityManager.createQuery("select c from Call c", Call.class).getResultList().get(0);
Phone phone = call.getPhone();
//tag::hql-collection-expressions-some-example[]
// the above query can be re-written with member of
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where :phone member of p.phones",
Person.class)
.setParameter("phone", phone)
.getResultList();
//end::hql-collection-expressions-some-example[]
assertEquals(1, persons.size());
});
}
@Test
public void test_hql_collection_expressions_example_6() {
doInJPA(this::entityManagerFactory, entityManager -> {
@ -1880,23 +1882,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
});
}
@Test
@SkipForDialect(value = DerbyDialect.class, comment = "Comparisons between 'DATE' and 'TIMESTAMP' are not supported")
public void test_hql_collection_expressions_example_7() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::hql-collection-expressions-example[]
List<Phone> phones = entityManager.createQuery(
"select p " +
"from Phone p " +
"where current_date() > key(p.callHistory)",
Phone.class)
.getResultList();
//end::hql-collection-expressions-example[]
assertEquals(1, phones.size());
});
}
@Test
@SkipForDialect(value = DerbyDialect.class, comment = "Comparisons between 'DATE' and 'TIMESTAMP' are not supported")
public void test_hql_collection_expressions_example_8() {
@ -1906,7 +1891,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = entityManager.createQuery(
"select p " +
"from Phone p " +
"where current_date() > all elements(p.repairTimestamps)",
"where local date > all elements(p.repairTimestamps)",
Phone.class)
.getResultList();
//end::hql-collection-expressions-all-example[]
@ -1933,15 +1918,15 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void test_hql_collection_expressions_example_10() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::hql-collection-expressions-example[]
//tag::hql-size-example[]
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where size(p.phones) = 2",
"where size(p.phones) >= 2",
Person.class)
.getResultList();
//end::hql-collection-expressions-example[]
//end::hql-size-example[]
assertEquals(1, persons.size());
});
}