clarify semantics of query with no 'select' in documentation

This commit is contained in:
Gavin King 2022-10-05 12:51:40 +02:00
parent 5f2d5e3938
commit a7bb19a2ea
2 changed files with 41 additions and 5 deletions

View File

@ -104,22 +104,32 @@ For example, the simplest query in HQL has no `select` clause at all:
[[hql-select-simplest-example]]
====
[source, SQL, indent=0]
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-select-simplest-example]
----
====
We don't necessarily _recommend_ leaving off the `select` list.
[NOTE]
====
JPQL requires a `select` clause, whereas HQL does not.
Naturally, the previous query may be written with a `select` clause:
[source, SQL, indent=0]
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-select-simplest-jpql-example]
----
When there's no explicit `select` clause, the select list is implied by the result type of the query:
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-select-no-from]
----
For complicated queries, it's probably best to explicitly specify a `select` list.
====
@ -142,7 +152,7 @@ Looking carefully at the BNF given above, you might notice that the `select` lis
Of course, standard SQL, and JPQL, require that the `select` list comes at the beginning.
But it's more natural to put it last:
[source, SQL, indent=0]
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-select-last-example]
----
@ -169,7 +179,7 @@ For example:
[[hql-update-example]]
====
[source, SQL, indent=0]
[source, JAVA, indent=0]
----
include::{sourcedir}/HQLTest.java[tags=hql-update-example]
----

View File

@ -155,7 +155,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void test_hql_select_simplest_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
List<Object> objects = session.createQuery(
@ -199,6 +198,33 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
});
}
@Test
public void test_hql_select_no_from() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
//tag::hql-select-no-from[]
// result type Person, only the Person selected
List<Person> persons = session.createQuery(
"from Person join phones", Person.class)
.getResultList();
for (Person person: persons) {
//...
}
// result type Object[], both Person and Phone selected
List<Object[]> personsWithPhones = session.createQuery(
"from Person join phones", Object[].class)
.getResultList();
for (Object[] personWithPhone: personsWithPhones) {
Person p = (Person) personWithPhone[0];
Phone ph = (Phone) personWithPhone[1];
//...
}
//end::hql-select-no-from[]
});
}
@Test
public void hql_update_example() {
doInJPA(this::entityManagerFactory, entityManager -> {