HHH-11827 - JPA entity native query not eagerly fetching associations as suggested by documentation.
This commit is contained in:
parent
7362a49477
commit
7f010569e5
|
@ -136,7 +136,8 @@ include::{sourcedir}/SQLTest.java[tags=sql-hibernate-entity-query-explicit-resul
|
|||
[[sql-entity-associations-query]]
|
||||
=== Handling associations and collections
|
||||
|
||||
If the entity is mapped with a `many-to-one` or a child-side `one-to-one` to another entity, it is required to also return this when performing the native query,
|
||||
If the entity is mapped with a `many-to-one` or a child-side `one-to-one` to another entity,
|
||||
it is required to also return this when performing the native query,
|
||||
otherwise a database specific _column not found_ error will occur.
|
||||
|
||||
[[sql-jpa-entity-associations-query-many-to-one-example]]
|
||||
|
@ -157,29 +158,11 @@ include::{sourcedir}/SQLTest.java[tags=sql-hibernate-entity-associations-query-m
|
|||
----
|
||||
====
|
||||
|
||||
This will allow the `Phone#person` to function properly.
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
The additional columns will automatically be returned when using the `*` notation.
|
||||
====
|
||||
This will allow the `Phone#person` to function properly since the `many-to-one` or `one-to-one`
|
||||
association is going to use a Proxy that will be initialized when being navigated for the first time.
|
||||
|
||||
It is possible to eagerly join the `Phone` and the `Person` entities to avoid the possible extra roundtrip for initializing the `many-to-one` association.
|
||||
|
||||
[[sql-jpa-entity-associations-query-many-to-one-join-example]]
|
||||
.JPA native query selecting entities with joined many-to-one association
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/SQLTest.java[tags=sql-jpa-entity-associations-query-many-to-one-join-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/sql-jpa-entity-associations-query-many-to-one-join-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
[[sql-hibernate-entity-associations-query-many-to-one-join-example]]
|
||||
.Hibernate native query selecting entities with joined many-to-one association
|
||||
====
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
SELECT id ,
|
||||
number ,
|
||||
type ,
|
||||
person_id
|
||||
FROM phone
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
Phone ph
|
||||
JOIN
|
||||
Person pr
|
||||
ON ph.person_id = pr.id
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
SELECT id ,
|
||||
number ,
|
||||
type ,
|
||||
person_id
|
||||
FROM phone
|
|
@ -43,6 +43,7 @@ import org.jboss.logging.Logger;
|
|||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
|
@ -345,7 +346,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
|
|||
.getResultList();
|
||||
|
||||
for(Phone phone : phones) {
|
||||
Person person = phone.getPerson();
|
||||
assertNotNull( phone.getPerson().getName() );
|
||||
}
|
||||
//end::sql-jpa-entity-associations-query-many-to-one-join-example[]
|
||||
assertEquals(3, phones.size());
|
||||
|
@ -368,6 +369,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
|
|||
for(Object[] tuple : tuples) {
|
||||
Phone phone = (Phone) tuple[0];
|
||||
Person person = (Person) tuple[1];
|
||||
assertNotNull( person.getName() );
|
||||
}
|
||||
//end::sql-hibernate-entity-associations-query-many-to-one-join-example[]
|
||||
assertEquals(3, tuples.size());
|
||||
|
|
Loading…
Reference in New Issue