clarify semantics of query with no 'select' in documentation
This commit is contained in:
parent
5f2d5e3938
commit
a7bb19a2ea
|
@ -104,22 +104,32 @@ For example, the simplest query in HQL has no `select` clause at all:
|
||||||
|
|
||||||
[[hql-select-simplest-example]]
|
[[hql-select-simplest-example]]
|
||||||
====
|
====
|
||||||
[source, SQL, indent=0]
|
[source, JAVA, indent=0]
|
||||||
----
|
----
|
||||||
include::{sourcedir}/HQLTest.java[tags=hql-select-simplest-example]
|
include::{sourcedir}/HQLTest.java[tags=hql-select-simplest-example]
|
||||||
----
|
----
|
||||||
====
|
====
|
||||||
|
|
||||||
|
We don't necessarily _recommend_ leaving off the `select` list.
|
||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
JPQL requires a `select` clause, whereas HQL does not.
|
JPQL requires a `select` clause, whereas HQL does not.
|
||||||
|
|
||||||
Naturally, the previous query may be written with a `select` clause:
|
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]
|
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.
|
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.
|
Of course, standard SQL, and JPQL, require that the `select` list comes at the beginning.
|
||||||
But it's more natural to put it last:
|
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]
|
include::{sourcedir}/HQLTest.java[tags=hql-select-last-example]
|
||||||
----
|
----
|
||||||
|
@ -169,7 +179,7 @@ For example:
|
||||||
|
|
||||||
[[hql-update-example]]
|
[[hql-update-example]]
|
||||||
====
|
====
|
||||||
[source, SQL, indent=0]
|
[source, JAVA, indent=0]
|
||||||
----
|
----
|
||||||
include::{sourcedir}/HQLTest.java[tags=hql-update-example]
|
include::{sourcedir}/HQLTest.java[tags=hql-update-example]
|
||||||
----
|
----
|
||||||
|
|
|
@ -155,7 +155,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_hql_select_simplest_example() {
|
public void test_hql_select_simplest_example() {
|
||||||
|
|
||||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||||
Session session = entityManager.unwrap(Session.class);
|
Session session = entityManager.unwrap(Session.class);
|
||||||
List<Object> objects = session.createQuery(
|
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
|
@Test
|
||||||
public void hql_update_example() {
|
public void hql_update_example() {
|
||||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||||
|
|
Loading…
Reference in New Issue