Skip to content

Fix LOCATE search exception while fetching data: Parameter 2 of function 'locate()' has type 'STRING', #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<joda-time.version>2.12.7</joda-time.version>
<graphql-java-extended-scalars.version>22.0</graphql-java-extended-scalars.version>
<jakarta.persistence-api.version>3.1.0</jakarta.persistence-api.version>
<hibernate.version>6.5.2.Final</hibernate.version>
<hibernate.version>6.5.3.Final</hibernate.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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.<String>get(filter.getField()), value.toString()), 0);
return cb.gt(cb.locate(field.as(String.class), value.toString()), 0);
} else {
Object object = value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -368,6 +368,23 @@ public void criteriaTesterLocate() {
assertThat(result).hasSize(1);
}

@Test
@Transactional
public void criteriaTesterLocateJson() {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<JsonEntity> criteria = builder.createQuery(JsonEntity.class);
Root<JsonEntity> json = criteria.from(JsonEntity.class);

criteria.select(json).where(builder.gt(builder.locate(json.<String>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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions schema/src/test/resources/GraphQLJpaConverterTests.sql
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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]}');
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}');
1 change: 1 addition & 0 deletions schema/src/test/resources/h2-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TYPE IF NOT EXISTS "JSONB" AS json;
4 changes: 2 additions & 2 deletions tests/gatling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

<properties>
<java.version>21</java.version>
<activiti-cloud.version>8.6.0</activiti-cloud.version>
<activiti-cloud.version>8.7.0</activiti-cloud.version>
<gatling.version>3.11.5</gatling.version>
<gatling-maven-plugin.version>4.9.6</gatling-maven-plugin.version>
<hibernate.version>6.4.10.Final</hibernate.version>
<hibernate.version>6.5.3.Final</hibernate.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion tests/gatling/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down