Skip to content

Fix where query criteria with multiple associations joins #485

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
May 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -1108,12 +1108,23 @@ protected Predicate getObjectFieldPredicate(

AbstractQuery<?> query = environment.getRoot();
Boolean isFetch = environment.getLocalContext();
Boolean isOptional = isOptionalAttribute(attribute);

From<?, ?> context = (isSubquery(query) || isCountQuery(query) || !isFetch)
? reuseJoin(from, objectField.getName(), isOptional)
: reuseFetch(from, objectField.getName(), isOptional);

boolean isOptional = isOptionalAttribute(attribute);
List<Map<String, Object>> logicalArguments = Optional
.ofNullable(environment.getArgument(logical.name()))
.filter(List.class::isInstance)
.map(List.class::cast)
.orElseGet(List::of);

From<?, ?> context;
if (logicalArguments.stream().filter(it -> it.containsKey(objectField.getName())).count() > 1) {
context =
isOptional ? from.join(objectField.getName(), JoinType.LEFT) : from.join(objectField.getName());
} else {
context =
(isSubquery(query) || isCountQuery(query) || !isFetch)
? reuseJoin(from, objectField.getName(), isOptional)
: reuseFetch(from, objectField.getName(), isOptional);
}
return getArgumentPredicate(
cb,
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,42 @@ public void criteriaTester3() {
assertThat(result).hasSize(1);
}

@Test
@Transactional
public void criteriaTesterMultipleJoinWhereCriteria() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();

CriteriaQuery<TaskEntity> tasksQuery = cb.createQuery(TaskEntity.class);
Root<TaskEntity> task = tasksQuery.from(TaskEntity.class);

Join<TaskEntity, TaskVariableEntity> taskVariableEntityJoin1 = task.join("variables");

Predicate var1 = cb.and(
cb.equal(taskVariableEntityJoin1.get("name"), "variable2"),
cb.equal(taskVariableEntityJoin1.get("value"), new VariableValue<>(Boolean.TRUE))
);

taskVariableEntityJoin1.on(var1);
taskVariableEntityJoin1.alias("var1");

Join<TaskEntity, TaskVariableEntity> taskVariableEntityJoin2 = task.join("variables");
Predicate var2 = cb.and(
cb.equal(taskVariableEntityJoin2.get("name"), "variable1"),
cb.equal(taskVariableEntityJoin2.get("value"), new VariableValue<>(new String("data")))
);

taskVariableEntityJoin2.on(var2);
taskVariableEntityJoin2.alias("var2");

tasksQuery.select(task);
// when:
List<TaskEntity> result = entityManager.createQuery(tasksQuery).getResultList();

// then:
assertThat(result).isNotEmpty();
assertThat(result).hasSize(1);
}

@Test
@Transactional
public void criteriaTester4() {
Expand Down Expand Up @@ -838,6 +874,51 @@ public void queryTasksVariablesWhereWithExplicitANDEXISTSByNameAndValueCriteria(
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryTasksVariablesWhereWithExplicitANDByMultipleNameAndValueCriteria() {
//given
String query =
"query {" +
" Tasks(where: {" +
" status: {EQ: COMPLETED}" +
" AND: [" +
" {" +
" variables: {" +
" name: {EQ: \"variable1\"}" +
" value: {EQ: \"data\"} }" +
" }" +
" {" +
" variables: {" +
" name: {EQ: \"variable2\"}" +
" value: {EQ: true} }" +
" }" +
" ]" +
" }) {" +
" select {" +
" id" +
" status" +
" variables {" +
" name" +
" value" +
" }" +
" }" +
" }" +
"}";

String expected =
"{Tasks={select=[" +
"{id=1, status=COMPLETED, variables=[" +
"{name=variable2, value=true}, " +
"{name=variable1, value=data}]}" +
"]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryTasksVariablesWhereWithEXISTSByNameAndValueCriteria() {
//given
Expand Down
7 changes: 7 additions & 0 deletions tests/gatling/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1090,3 +1090,10 @@ insert into INTEGRATION_CONTEXT (id, client_id, client_name, execution_id, proce
('2', '1', 'serviceTask', '2', '1'),
('3', '1', 'serviceTask', '3', '1');

insert into TASK_PROCESS_VARIABLE (task_id, process_variable_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6);
Loading