Skip to content

Commit 3da8d85

Browse files
reda-alaouisebersole
authored andcommitted
HHH-9374 EntityGraph applied to subquery when using collection function
(cherry picked from commit 1677e06)
1 parent c84cc32 commit 3da8d85

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

hibernate-core/src/main/java/org/hibernate/engine/query/spi/EntityGraphQueryHint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.engine.query.spi;
88

99
import java.util.ArrayList;
10+
import java.util.Collections;
1011
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Map;
@@ -52,7 +53,8 @@ public List<FromElement> toFromElements(FromClause fromClause, HqlSqlWalker walk
5253
}
5354

5455
return getFromElements(
55-
originEntityGraph.getAttributeNodes(),
56+
fromClause.getLevel() == FromClause.ROOT_LEVEL ? originEntityGraph.getAttributeNodes():
57+
Collections.emptyList(),
5658
fromClause.getFromElement(),
5759
fromClause,
5860
walker,

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/queryhint/QueryHintEntityGraphTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void testEntityGraphWithExplicitFetchAndRestriction() {
281281
// Ensure the EntityGraph and explicit fetches do not conflict.
282282
Query query = entityManager.createQuery( "from " + Company.class.getName()
283283
+ " as c left join fetch c.location left join fetch c.employees where c.location.zip = :zip")
284-
.setParameter( "zip", 12345 );
284+
.setParameter("zip", 12345);
285285
query.setHint( QueryHints.HINT_LOADGRAPH, entityGraph );
286286
Company company = (Company) query.getSingleResult();
287287

@@ -296,6 +296,22 @@ public void testEntityGraphWithExplicitFetchAndRestriction() {
296296
assertTrue( Hibernate.isInitialized( company.phoneNumbers ) );
297297
}
298298

299+
@Test
300+
@TestForIssue(jiraKey = "HHH-9374")
301+
public void testEntityGraphWithCollectionSubquery(){
302+
EntityManager entityManager = getOrCreateEntityManager();
303+
entityManager.getTransaction().begin();
304+
305+
EntityGraph<Company> entityGraph = entityManager.createEntityGraph(Company.class);
306+
entityGraph.addAttributeNodes("location");
307+
Query query = entityManager.createQuery("select c from " + Company.class.getName() + " c where c.employees IS EMPTY");
308+
query.setHint(QueryHints.HINT_LOADGRAPH, entityGraph);
309+
query.getResultList();
310+
311+
entityManager.getTransaction().commit();
312+
entityManager.close();
313+
}
314+
299315
@Before
300316
public void createData() {
301317
EntityManager entityManager = getOrCreateEntityManager();

0 commit comments

Comments
 (0)