Skip to content

Commit 3aa3347

Browse files
committedMay 10, 2019
fix: TODO plural predicate fetch join detection
1 parent 4862ad0 commit 3aa3347

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed
 

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
import java.util.Collection;
2424
import java.util.Date;
2525
import java.util.EnumSet;
26-
import java.util.List;
2726
import java.util.Set;
2827
import java.util.UUID;
2928

3029
import javax.persistence.criteria.CriteriaBuilder;
3130
import javax.persistence.criteria.Expression;
3231
import javax.persistence.criteria.Fetch;
3332
import javax.persistence.criteria.From;
34-
import javax.persistence.criteria.Join;
3533
import javax.persistence.criteria.Path;
3634
import javax.persistence.criteria.Predicate;
35+
import javax.persistence.metamodel.EntityType;
36+
import javax.persistence.metamodel.PluralAttribute;
3737

3838
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
3939
import graphql.language.NullValue;
@@ -416,17 +416,17 @@ else if (type.equals(UUID.class)) {
416416
}
417417
else if(Collection.class.isAssignableFrom(type)) {
418418
// collection join for plural attributes
419-
// TODO need better detection
420-
if(field.getModel() == null) {
421-
Join<?,?> join = null;
419+
if(PluralAttribute.class.isInstance(from.getModel())
420+
|| EntityType.class.isInstance(from.getModel())) {
421+
From<?,?> join = null;
422422

423423
for(Fetch<?,?> fetch: from.getFetches()) {
424424
if(fetch.getAttribute().getName().equals(filter.getField()))
425-
join = (Join<?,?>) fetch;
425+
join = (From<?,?>) fetch;
426426
}
427427

428428
if(join == null) {
429-
join = (Join<?,?>) from.fetch(filter.getField());
429+
join = (From<?,?>) from.fetch(filter.getField());
430430
}
431431

432432
Predicate in = join.in(value);
@@ -446,7 +446,7 @@ else if (Object.class.isAssignableFrom(type)) {
446446
else {
447447
Object object = value;
448448

449-
if(List.class.isInstance(value)) {
449+
if(Collection.class.isInstance(value)) {
450450
object = getValues(object, type);
451451
} else {
452452
object = getValue(object, type);
@@ -468,8 +468,8 @@ else if (filter.getCriterias().contains(PredicateFilter.Criteria.IN)
468468
) {
469469
CriteriaBuilder.In<Object> in = cb.in(from.get(filter.getField()));
470470

471-
if(List.class.isInstance(object)) {
472-
List.class.cast(object)
471+
if(Collection.class.isInstance(object)) {
472+
Collection.class.cast(object)
473473
.forEach(in::value);
474474
} else {
475475
in.value(object);
@@ -502,9 +502,9 @@ private Object getValue(Object object, Class<?> type) {
502502
}
503503

504504
private Object getValues(Object object, Class<?> type) {
505-
List<Object> objects = new ArrayList<>();
505+
Collection<Object> objects = new ArrayList<>();
506506

507-
for (Object value : List.class.cast(object).toArray()) {
507+
for (Object value : Collection.class.cast(object).toArray()) {
508508
objects.add(getValue(value, type));
509509
}
510510

0 commit comments

Comments
 (0)
Please sign in to comment.