Skip to content

Commit

Permalink
Merge pull request #133 from croz-ltd/feature_supportJoinByCustomIdName
Browse files Browse the repository at this point in the history
Feature support join by custom id name
  • Loading branch information
jzrilic authored Sep 15, 2022
2 parents 09a56e0 + f711e6e commit fafc009
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,4 @@ public class SearchPropertyJoin {
*/
private final String childProperty;

public static SearchPropertyJoin defaultJoinById() {
return new SearchPropertyJoin("id", "id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ private <P, R> List<Predicate> resolveQueryPredicateList(R request, SearchConfig
mainQueryPredicateList.addAll(convertRestrictionListToPredicateList(pluralRestrictionList, root, criteriaBuilder));
}
else {
Subquery<Integer> subquery = createSubqueryRestriction(root.getJavaType(), root, query, criteriaBuilder, pluralRestrictionList, SearchPropertyJoin.defaultJoinById());
SearchPropertyJoin searchPropertyJoin = resolveSearchPropertyJoin(root);
Subquery<Integer> subquery = createSubqueryRestriction(root.getJavaType(), root, query, criteriaBuilder, pluralRestrictionList, searchPropertyJoin);

mainQueryPredicateList.add(criteriaBuilder.exists(subquery));
}
Expand Down Expand Up @@ -275,6 +276,12 @@ private List<Predicate> convertRestrictionListToPredicateList(Collection<Restric
return predicateList;
}

private SearchPropertyJoin resolveSearchPropertyJoin(Root<?> root) {
String idName = root.getModel().getId(root.getModel().getIdType().getJavaType()).getName();

return new SearchPropertyJoin(idName, idName);
}

// TODO enable join usage or subquery?
private <R> List<Subquery<?>> resolveSubqueryList(R request, SearchPropertyConfiguration searchPropertyConfiguration, List<SubqueryConfiguration> subqueryConfigurationList, Root<?> root,
CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020-2022 CROZ d.o.o, the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package net.croz.nrich.search.repository.stub;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.List;

@Setter
@Getter
@Entity
public class TestEntityWithCustomId {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long customId;

@Enumerated(EnumType.STRING)
@ElementCollection
private List<TestEntityEnum> enumElementCollection;

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.croz.nrich.search.repository.stub.TestEntityEnum;
import net.croz.nrich.search.repository.stub.TestEntityProjectionDto;
import net.croz.nrich.search.repository.stub.TestEntitySearchRequest;
import net.croz.nrich.search.repository.stub.TestEntityWithCustomId;
import net.croz.nrich.search.repository.stub.TestEntityWithEmbeddedId;
import net.croz.nrich.search.support.JpaQueryBuilder;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -54,6 +55,7 @@

import static net.croz.nrich.search.repository.testutil.JpaSearchRepositoryExecutorGeneratingUtil.createTestEntitySearchRequestJoin;
import static net.croz.nrich.search.repository.testutil.JpaSearchRepositoryExecutorGeneratingUtil.generateListForSearch;
import static net.croz.nrich.search.repository.testutil.JpaSearchRepositoryExecutorGeneratingUtil.generateTestEntityWithCustomIdList;
import static net.croz.nrich.search.repository.testutil.JpaSearchRepositoryExecutorGeneratingUtil.generateTestEntityWithEmbeddedIdList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand Down Expand Up @@ -727,6 +729,26 @@ void shouldFindByElementCollection() {
assertThat(results).hasSize(1);
}

@Test
void shouldSupportJoinsByDifferentIdProperty() {
// given
generateTestEntityWithCustomIdList(entityManager);

JpaQueryBuilder<TestEntityWithCustomId> testEntityWithCustomIdQueryBuilder = new JpaQueryBuilder<>(entityManager, TestEntityWithCustomId.class);

Map<String, Object> mapSearchRequest = new HashMap<>();
mapSearchRequest.put("enumElementCollection", TestEntityEnum.FIRST);

SearchConfiguration<TestEntityWithCustomId, TestEntityWithCustomId, Map<String, Object>> searchConfiguration = SearchConfiguration.<TestEntityWithCustomId, TestEntityWithCustomId, Map<String, Object>>builder()
.build();

// when
List<TestEntityWithCustomId> results = entityManager.createQuery(testEntityWithCustomIdQueryBuilder.buildQuery(mapSearchRequest, searchConfiguration, Sort.unsorted())).getResultList();

// then
assertThat(results).hasSize(2);
}

private <P, R> List<P> executeQuery(R request, SearchConfiguration<TestEntity, P, R> searchConfiguration) {
return executeQuery(request, searchConfiguration, Sort.unsorted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.croz.nrich.search.repository.stub.TestEntityEmbedded;
import net.croz.nrich.search.repository.stub.TestEntityEnum;
import net.croz.nrich.search.repository.stub.TestEntitySearchRequest;
import net.croz.nrich.search.repository.stub.TestEntityWithCustomId;
import net.croz.nrich.search.repository.stub.TestEntityWithEmbeddedId;
import net.croz.nrich.search.repository.stub.TestNestedEntity;
import net.croz.nrich.search.repository.stub.TestStringSearchEntity;
Expand All @@ -35,6 +36,7 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -114,6 +116,16 @@ public static SearchJoin<TestEntitySearchRequest> createTestEntitySearchRequestJ
.build();
}

public static void generateTestEntityWithCustomIdList(EntityManager entityManager) {
IntStream.range(0, 3).forEach(value -> {
TestEntityWithCustomId entity = new TestEntityWithCustomId();

entity.setEnumElementCollection(Collections.singletonList(value % 2 == 0 ? TestEntityEnum.FIRST : TestEntityEnum.SECOND));

entityManager.persist(entity);
});
}

private static TestEntity createTestEntity(Integer value, Integer numberOfCollectionEntities) {
TestNestedEntity nestedEntity = createTestNestedEntity(value);
List<TestCollectionEntity> collectionEntityList = IntStream.range(0, numberOfCollectionEntities)
Expand Down

0 comments on commit fafc009

Please sign in to comment.