Skip to content

Commit 6e204fc

Browse files
authored
fix(GH-216): One-to-many queries joins the same tables twice (#217)
1 parent 0151eec commit 6e204fc

File tree

3 files changed

+1
-56
lines changed

3 files changed

+1
-56
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaQueryDataFetcher.java

-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Optional;
2222
import java.util.stream.Collectors;
2323

24-
import javax.persistence.EntityGraph;
2524
import javax.persistence.EntityManager;
2625
import javax.persistence.TypedQuery;
2726
import javax.persistence.criteria.CriteriaBuilder;
@@ -117,14 +116,6 @@ public Object get(DataFetchingEnvironment environment) {
117116
.setFirstResult((page.page - 1) * page.size);
118117
}
119118

120-
// Let's create entity graph from selection
121-
// When using fetchgraph all relationships are considered to be lazy regardless of annotation,
122-
// and only the elements of the provided graph are loaded. This particularly useful when running
123-
// reports on certain objects and you don't want a lot of the stuff that's normally flagged to
124-
// load via eager annotations.
125-
EntityGraph<?> graph = buildEntityGraph(queryField);
126-
query.setHint(JAVAX_PERSISTENCE_FETCHGRAPH, graph);
127-
128119
// Let' try reduce overhead and disable all caching
129120
query.setHint(ORG_HIBERNATE_READ_ONLY, true);
130121
query.setHint(ORG_HIBERNATE_FETCH_SIZE, 1000);

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSimpleDataFetcher.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.stream.Collectors;
2121
import java.util.stream.Stream;
2222

23-
import javax.persistence.EntityGraph;
2423
import javax.persistence.EntityManager;
2524
import javax.persistence.NoResultException;
2625
import javax.persistence.metamodel.EntityType;
@@ -48,12 +47,7 @@ public Object get(DataFetchingEnvironment environment) {
4847
flattenEmbeddedIdArguments(field);
4948

5049
try {
51-
// Create entity graph from selection
52-
EntityGraph<?> entityGraph = buildEntityGraph(field);
53-
54-
return super.getQuery(environment, field, true)
55-
.setHint("javax.persistence.fetchgraph", entityGraph)
56-
.getSingleResult();
50+
return getQuery(environment, field, true).getSingleResult();
5751

5852
} catch (NoResultException ignored) {
5953
// do nothing

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/QraphQLJpaBaseDataFetcher.java

-40
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
import java.util.stream.IntStream;
4141
import java.util.stream.Stream;
4242

43-
import javax.persistence.EntityGraph;
4443
import javax.persistence.EntityManager;
45-
import javax.persistence.Subgraph;
4644
import javax.persistence.TypedQuery;
4745
import javax.persistence.criteria.AbstractQuery;
4846
import javax.persistence.criteria.CriteriaBuilder;
@@ -1145,44 +1143,6 @@ protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObject
11451143
}
11461144
return fieldDefinition;
11471145
}
1148-
1149-
protected Subgraph<?> buildSubgraph(Field field, Subgraph<?> subgraph) {
1150-
1151-
selections(field).forEach(it ->{
1152-
if(hasSelectionSet(it)) {
1153-
Subgraph<?> sg = subgraph.addSubgraph(it.getName());
1154-
buildSubgraph(it, sg);
1155-
} else {
1156-
if(!TYPENAME.equals(it.getName()))
1157-
subgraph.addAttributeNodes(it.getName());
1158-
}
1159-
});
1160-
1161-
return subgraph;
1162-
};
1163-
1164-
1165-
protected EntityGraph<?> buildEntityGraph(Field root) {
1166-
1167-
EntityGraph<?> entityGraph = this.entityManager.createEntityGraph(entityType.getJavaType());
1168-
1169-
selections(root)
1170-
.forEach(it -> {
1171-
if(hasSelectionSet(it)
1172-
&& hasNoArguments(it)
1173-
&& isManagedType(entityType.getAttribute(it.getName()))
1174-
) {
1175-
Subgraph<?> sg = entityGraph.addSubgraph(it.getName());
1176-
buildSubgraph(it, sg);
1177-
} else {
1178-
if(isPersistent(entityType, it.getName()))
1179-
entityGraph.addAttributeNodes(it.getName());
1180-
}
1181-
});
1182-
1183-
return entityGraph;
1184-
};
1185-
11861146

11871147
protected final boolean isManagedType(Attribute<?,?> attribute) {
11881148
return attribute.getPersistentAttributeType() != PersistentAttributeType.EMBEDDED

0 commit comments

Comments
 (0)