Skip to content

Commit 3f9704b

Browse files
authored
feat: add LOCATE predicate for JPA entity attributes annotated with @convert (#115)
* feat: add LOCATE predicate for attributes annotated with @convert * fix: polish test data * fix: clean up old code.
1 parent 1955ab6 commit 3f9704b

16 files changed

+860
-9
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ target/
2121
.project
2222
.springBeans
2323
.classpath
24+
25+
.externalToolBuilders
26+

graphql-jpa-query-schema/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
<scope>test</scope>
6161
</dependency>
6262

63+
<dependency>
64+
<groupId>com.fasterxml.jackson.core</groupId>
65+
<artifactId>jackson-databind</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
6369
</dependencies>
6470

6571
</project>

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

-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public Object get(DataFetchingEnvironment environment) {
6666

6767
//EntityGraph<?> entityGraph = buildEntityGraph(new Field("select", new SelectionSet(Arrays.asList(field))));
6868

69-
// Let's clear session persistent context to avoid getting stale objects cached in the same session
70-
// between requests with different search criteria. This looks like a Hibernate bug...
71-
entityManager.clear();
72-
7369
return getQuery(environment, field, true)
7470
//.setHint("javax.persistence.fetchgraph", entityGraph) // TODO: fix runtime exception
7571
.getResultList();

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Collectors;
3434
import java.util.stream.Stream;
3535

36+
import javax.persistence.Convert;
3637
import javax.persistence.EntityManager;
3738
import javax.persistence.Transient;
3839
import javax.persistence.metamodel.Attribute;
@@ -43,9 +44,6 @@
4344
import javax.persistence.metamodel.SingularAttribute;
4445
import javax.persistence.metamodel.Type;
4546

46-
import org.slf4j.Logger;
47-
import org.slf4j.LoggerFactory;
48-
4947
import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
5048
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
5149
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
@@ -55,7 +53,6 @@
5553
import com.introproventures.graphql.jpa.query.schema.NamingStrategy;
5654
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.CachedIntrospectionResult.CachedPropertyDescriptor;
5755
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
58-
5956
import graphql.Assert;
6057
import graphql.Scalars;
6158
import graphql.schema.Coercing;
@@ -73,6 +70,8 @@
7370
import graphql.schema.GraphQLType;
7471
import graphql.schema.GraphQLTypeReference;
7572
import graphql.schema.PropertyDataFetcher;
73+
import org.slf4j.Logger;
74+
import org.slf4j.LoggerFactory;
7675

7776
/**
7877
* JPA specific schema builder implementation of {code #GraphQLSchemaBuilder} interface
@@ -437,6 +436,16 @@ private GraphQLInputType getWhereAttributeType(Attribute<?,?> attribute) {
437436
.type(getAttributeInputType(attribute))
438437
.build()
439438
);
439+
}
440+
else if (attribute.getJavaMember().getClass().isAssignableFrom(Field.class)
441+
&& Field.class.cast(attribute.getJavaMember())
442+
.isAnnotationPresent(Convert.class))
443+
{
444+
builder.field(GraphQLInputObjectField.newInputObjectField()
445+
.name(Criteria.LOCATE.name())
446+
.description("Locate search criteria")
447+
.type(getAttributeInputType(attribute))
448+
.build());
440449
}
441450
}
442451

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

+3
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ else if(Collection.class.isAssignableFrom(type)) {
414414
} else if(type.isEnum()) {
415415
return getEnumPredicate((Path<Enum<?>>) field, predicateFilter);
416416
}
417+
else if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
418+
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
419+
}
417420

418421
throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());
419422
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public enum Criteria {
103103
/**
104104
* Not Between condition
105105
*/
106-
NOT_BETWEEN;
106+
NOT_BETWEEN,
107+
108+
/**
109+
* JPA's LOCATE predicate for attributes annotated with @Convert
110+
*/
111+
LOCATE;
107112

108113
private static Set<String> names = EnumSet.allOf(Criteria.class)
109114
.stream()

0 commit comments

Comments
 (0)