diff --git a/dependencies/pom.xml b/dependencies/pom.xml index 79915e4bb..a4b56b707 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -20,7 +20,7 @@ 2.12.7 22.0 3.1.0 - 6.5.2.Final + 6.5.3.Final diff --git a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java index 9e2997f1b..af4a40c41 100644 --- a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java +++ b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java @@ -18,6 +18,7 @@ import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria; import graphql.language.NullValue; +import jakarta.persistence.Column; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.Expression; import jakarta.persistence.criteria.From; @@ -44,6 +45,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -825,7 +827,7 @@ private Predicate getTypedPredicate(From from, Path field, PredicateFil } // TODO need better detection mechanism else if (Object.class.isAssignableFrom(type)) { if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) { - return cb.gt(cb.locate(from.get(filter.getField()), value.toString()), 0); + return cb.gt(cb.locate(field.as(String.class), value.toString()), 0); } else { Object object = value; diff --git a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java index acf433900..f4d44ac68 100644 --- a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java +++ b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java @@ -50,7 +50,7 @@ @SpringBootTest( properties = { "spring.sql.init.data-locations=GraphQLJpaConverterTests.sql", - "spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE", + "spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE;INIT=RUNSCRIPT FROM 'classpath:h2-init.sql'", } ) public class GraphQLJpaConverterTests extends AbstractSpringBootTestSupport { @@ -368,6 +368,23 @@ public void criteriaTesterLocate() { assertThat(result).hasSize(1); } + @Test + @Transactional + public void criteriaTesterLocateJson() { + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteria = builder.createQuery(JsonEntity.class); + Root json = criteria.from(JsonEntity.class); + + criteria.select(json).where(builder.gt(builder.locate(json.get("json").as(String.class), "key"), 0)); + + // when: + List result = entityManager.createQuery(criteria).getResultList(); + + // then: + assertThat(result).isNotEmpty(); + assertThat(result).hasSize(1); + } + @Test public void queryJsonEntity() { //given @@ -425,6 +442,35 @@ public void queryJsonEntityWhereSearchCriteria() { assertThat(result.toString()).isEqualTo(expected); } + @Test + public void queryJsonEntityWhereSearchCriteriaJsonb() { + //given + String query = + "query {" + + " JsonEntities(where: {" + + "json: {LOCATE: \"key\"}" + + "}) {" + + " select {" + + " id" + + " firstName" + + " lastName" + + " json" + + " }" + + " }" + + "}"; + + String expected = + "{JsonEntities={select=[" + + "{id=1, firstName=john, lastName=doe, json=\"{\\\"attr\\\":{\\\"key\\\":[\\\"1\\\",\\\"2\\\",\\\"3\\\",\\\"4\\\",\\\"5\\\"]}}\"}" + + "]}}"; + + //when + Object result = executor.execute(query).getData(); + + // then + assertThat(result.toString()).isEqualTo(expected); + } + @Test public void queryTaskVariablesWhereSearchCriteria() { //given diff --git a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java index 4aec5ad8d..51b859148 100644 --- a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java +++ b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java @@ -49,7 +49,7 @@ @SpringBootTest( properties = { "spring.sql.init.data-locations=GraphQLJpaAggregateTests.sql", - "spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE", + "spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE;INIT=RUNSCRIPT FROM 'classpath:h2-init.sql'", } ) public class GraphQLJpaQueryAggregateTests extends AbstractSpringBootTestSupport { diff --git a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/model/JsonEntity.java b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/model/JsonEntity.java index d5aac6468..6b93fc71f 100644 --- a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/model/JsonEntity.java +++ b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/model/JsonEntity.java @@ -23,4 +23,8 @@ public class JsonEntity { @Convert(converter = JsonNodeConverter.class) @Column(columnDefinition = "text") private JsonNode attributes; + + @Convert(converter = JsonNodeConverter.class) + @Column(columnDefinition = "jsonb") + private JsonNode json; } diff --git a/schema/src/test/resources/GraphQLJpaConverterTests.sql b/schema/src/test/resources/GraphQLJpaConverterTests.sql index 2d4743661..4c85be6dc 100644 --- a/schema/src/test/resources/GraphQLJpaConverterTests.sql +++ b/schema/src/test/resources/GraphQLJpaConverterTests.sql @@ -1,7 +1,7 @@ -- Json entity -insert into json_entity (id, first_name, last_name, attributes) values - (1, 'john', 'doe', '{"attr":{"key":["1","2","3","4","5"]}}'), - (2, 'joe', 'smith', '{"attr":["1","2","3","4","5"]}'); +insert into json_entity (id, first_name, last_name, attributes, json) values + (1, 'john', 'doe', '{"attr":{"key":["1","2","3","4","5"]}}','{"attr":{"key":["1","2","3","4","5"]}}'), + (2, 'joe', 'smith', '{"attr":["1","2","3","4","5"]}', '{"attr":["1","2","3","4","5"]}'); insert into task (id, assignee, business_key, created_date, description, due_date, last_modified, last_modified_from, last_modified_to, name, priority, process_definition_id, process_instance_id, status, owner, claimed_date) values ('1', 'assignee', 'bk1', CURRENT_TIMESTAMP, 'description', null, null, null, null, 'task1', 5, 'process_definition_id', 0, 'COMPLETED' , 'owner', null), @@ -21,4 +21,4 @@ insert into TASK_VARIABLE (create_time, execution_id, last_updated_time, name, p (CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable4', 0, '2', 'json', '{"value":{"key":"data"}}'), (CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.2345}'), (CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'int', '{"value":12345}'), - (CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}'); \ No newline at end of file + (CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}'); diff --git a/schema/src/test/resources/h2-init.sql b/schema/src/test/resources/h2-init.sql new file mode 100644 index 000000000..ad44c9ede --- /dev/null +++ b/schema/src/test/resources/h2-init.sql @@ -0,0 +1 @@ +CREATE TYPE IF NOT EXISTS "JSONB" AS json; diff --git a/tests/gatling/pom.xml b/tests/gatling/pom.xml index ac938b318..8df454cd9 100644 --- a/tests/gatling/pom.xml +++ b/tests/gatling/pom.xml @@ -12,10 +12,10 @@ 21 - 8.6.0 + 8.7.0 3.11.5 4.9.6 - 6.4.10.Final + 6.5.3.Final diff --git a/tests/gatling/src/main/resources/application.yml b/tests/gatling/src/main/resources/application.yml index 09a11943a..9ff51aada 100644 --- a/tests/gatling/src/main/resources/application.yml +++ b/tests/gatling/src/main/resources/application.yml @@ -12,7 +12,7 @@ spring: jpa: hibernate.ddl-auto: validate generate-ddl: false - show-sql: false + show-sql: true defer-datasource-initialization: false open-in-view: false database-platform: org.hibernate.dialect.PostgreSQLDialect