Skip to content

Commit 05f5563

Browse files
authored
Fix LOCATE search exception while fetching data: Parameter 2 of function 'locate()' has type 'STRING', (#523)
1 parent e1da620 commit 05f5563

File tree

9 files changed

+64
-11
lines changed

9 files changed

+64
-11
lines changed

dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<joda-time.version>2.12.7</joda-time.version>
2121
<graphql-java-extended-scalars.version>22.0</graphql-java-extended-scalars.version>
2222
<jakarta.persistence-api.version>3.1.0</jakarta.persistence-api.version>
23-
<hibernate.version>6.5.2.Final</hibernate.version>
23+
<hibernate.version>6.5.3.Final</hibernate.version>
2424
</properties>
2525

2626
<dependencyManagement>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
2020
import graphql.language.NullValue;
21+
import jakarta.persistence.Column;
2122
import jakarta.persistence.criteria.CriteriaBuilder;
2223
import jakarta.persistence.criteria.Expression;
2324
import jakarta.persistence.criteria.From;
@@ -44,6 +45,7 @@
4445
import java.util.LinkedHashSet;
4546
import java.util.List;
4647
import java.util.Map;
48+
import java.util.Optional;
4749
import java.util.Set;
4850
import java.util.UUID;
4951

@@ -825,7 +827,7 @@ private Predicate getTypedPredicate(From<?, ?> from, Path<?> field, PredicateFil
825827
} // TODO need better detection mechanism
826828
else if (Object.class.isAssignableFrom(type)) {
827829
if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
828-
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
830+
return cb.gt(cb.locate(field.as(String.class), value.toString()), 0);
829831
} else {
830832
Object object = value;
831833

schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
@SpringBootTest(
5151
properties = {
5252
"spring.sql.init.data-locations=GraphQLJpaConverterTests.sql",
53-
"spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE",
53+
"spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE;INIT=RUNSCRIPT FROM 'classpath:h2-init.sql'",
5454
}
5555
)
5656
public class GraphQLJpaConverterTests extends AbstractSpringBootTestSupport {
@@ -368,6 +368,23 @@ public void criteriaTesterLocate() {
368368
assertThat(result).hasSize(1);
369369
}
370370

371+
@Test
372+
@Transactional
373+
public void criteriaTesterLocateJson() {
374+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
375+
CriteriaQuery<JsonEntity> criteria = builder.createQuery(JsonEntity.class);
376+
Root<JsonEntity> json = criteria.from(JsonEntity.class);
377+
378+
criteria.select(json).where(builder.gt(builder.locate(json.<String>get("json").as(String.class), "key"), 0));
379+
380+
// when:
381+
List<?> result = entityManager.createQuery(criteria).getResultList();
382+
383+
// then:
384+
assertThat(result).isNotEmpty();
385+
assertThat(result).hasSize(1);
386+
}
387+
371388
@Test
372389
public void queryJsonEntity() {
373390
//given
@@ -425,6 +442,35 @@ public void queryJsonEntityWhereSearchCriteria() {
425442
assertThat(result.toString()).isEqualTo(expected);
426443
}
427444

445+
@Test
446+
public void queryJsonEntityWhereSearchCriteriaJsonb() {
447+
//given
448+
String query =
449+
"query {" +
450+
" JsonEntities(where: {" +
451+
"json: {LOCATE: \"key\"}" +
452+
"}) {" +
453+
" select {" +
454+
" id" +
455+
" firstName" +
456+
" lastName" +
457+
" json" +
458+
" }" +
459+
" }" +
460+
"}";
461+
462+
String expected =
463+
"{JsonEntities={select=[" +
464+
"{id=1, firstName=john, lastName=doe, json=\"{\\\"attr\\\":{\\\"key\\\":[\\\"1\\\",\\\"2\\\",\\\"3\\\",\\\"4\\\",\\\"5\\\"]}}\"}" +
465+
"]}}";
466+
467+
//when
468+
Object result = executor.execute(query).getData();
469+
470+
// then
471+
assertThat(result.toString()).isEqualTo(expected);
472+
}
473+
428474
@Test
429475
public void queryTaskVariablesWhereSearchCriteria() {
430476
//given

schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
@SpringBootTest(
5050
properties = {
5151
"spring.sql.init.data-locations=GraphQLJpaAggregateTests.sql",
52-
"spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE",
52+
"spring.datasource.url=jdbc:h2:mem:db;NON_KEYWORDS=VALUE;INIT=RUNSCRIPT FROM 'classpath:h2-init.sql'",
5353
}
5454
)
5555
public class GraphQLJpaQueryAggregateTests extends AbstractSpringBootTestSupport {

schema/src/test/java/com/introproventures/graphql/jpa/query/converter/model/JsonEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public class JsonEntity {
2323
@Convert(converter = JsonNodeConverter.class)
2424
@Column(columnDefinition = "text")
2525
private JsonNode attributes;
26+
27+
@Convert(converter = JsonNodeConverter.class)
28+
@Column(columnDefinition = "jsonb")
29+
private JsonNode json;
2630
}

schema/src/test/resources/GraphQLJpaConverterTests.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Json entity
2-
insert into json_entity (id, first_name, last_name, attributes) values
3-
(1, 'john', 'doe', '{"attr":{"key":["1","2","3","4","5"]}}'),
4-
(2, 'joe', 'smith', '{"attr":["1","2","3","4","5"]}');
2+
insert into json_entity (id, first_name, last_name, attributes, json) values
3+
(1, 'john', 'doe', '{"attr":{"key":["1","2","3","4","5"]}}','{"attr":{"key":["1","2","3","4","5"]}}'),
4+
(2, 'joe', 'smith', '{"attr":["1","2","3","4","5"]}', '{"attr":["1","2","3","4","5"]}');
55

66
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
77
('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
2121
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable4', 0, '2', 'json', '{"value":{"key":"data"}}'),
2222
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.2345}'),
2323
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'int', '{"value":12345}'),
24-
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}');
24+
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}');

schema/src/test/resources/h2-init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TYPE IF NOT EXISTS "JSONB" AS json;

tests/gatling/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
<properties>
1414
<java.version>21</java.version>
15-
<activiti-cloud.version>8.6.0</activiti-cloud.version>
15+
<activiti-cloud.version>8.7.0</activiti-cloud.version>
1616
<gatling.version>3.11.5</gatling.version>
1717
<gatling-maven-plugin.version>4.9.6</gatling-maven-plugin.version>
18-
<hibernate.version>6.4.10.Final</hibernate.version>
18+
<hibernate.version>6.5.3.Final</hibernate.version>
1919
</properties>
2020

2121
<dependencies>

tests/gatling/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spring:
1212
jpa:
1313
hibernate.ddl-auto: validate
1414
generate-ddl: false
15-
show-sql: false
15+
show-sql: true
1616
defer-datasource-initialization: false
1717
open-in-view: false
1818
database-platform: org.hibernate.dialect.PostgreSQLDialect

0 commit comments

Comments
 (0)