Skip to content

Commit

Permalink
Fix issue with collection attributes not being recognized and using a…
Browse files Browse the repository at this point in the history
… join instead of exsits query by default
  • Loading branch information
agrancaric committed Sep 20, 2024
1 parent 0e0a54d commit 588bc34
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private Set<Restriction> resolveRestrictionListInternal(MapSupportingDirectField
}

// element collections have null managed type but should be treated as plural attributes
boolean isCurrentAttributePlural = attributeHolder.isElementCollection() || isPluralAttribute;
boolean isCurrentAttributePlural = attributeHolder.isPlural() || attributeHolder.isElementCollection() || isPluralAttribute;

restrictionList.add(createAttributeRestriction(attributeHolder.getAttribute().getJavaType(), originalFieldName, currentPath, value, isCurrentAttributePlural));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.List;
import java.util.Map;

import static net.croz.nrich.search.repository.testutil.JpaSearchRepositoryExecutorGeneratingUtil.createTestEntityForCollectionSearch;
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;
Expand Down Expand Up @@ -872,11 +873,28 @@ void shouldSupportSubclassAttributeSearch() {
assertThat(results).hasSize(1);
}

@Test
void shouldCountCorrectlyWhenSearchingByCollection() {
// given
TestEntity entity = createTestEntityForCollectionSearch(entityManager);
Map<String, Object> requestMap = Map.of("name", entity.getName(), "collectionEntityList.name", "collection");

// when
Long result = executeCountQuery(requestMap, SearchConfiguration.emptyConfigurationWithDefaultMappingResolve());

// then
assertThat(result).isEqualTo(1L);
}

private <P, R> List<P> executeQuery(R request, SearchConfiguration<TestEntity, P, R> searchConfiguration) {
return executeQuery(request, searchConfiguration, Sort.unsorted());
}

private <P, R> List<P> executeQuery(R request, SearchConfiguration<TestEntity, P, R> searchConfiguration, Sort sort) {
return entityManager.createQuery(jpaQueryBuilder.buildQuery(request, searchConfiguration, sort)).getResultList();
}

private <P, R> Long executeCountQuery(R request, SearchConfiguration<TestEntity, P, R> searchConfiguration) {
return entityManager.createQuery(jpaQueryBuilder.buildCountQuery(request, searchConfiguration)).getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import jakarta.persistence.criteria.JoinType;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -137,6 +136,16 @@ public static void generateTestSubEntityList(EntityManager entityManager) {
});
}

public static TestEntity createTestEntityForCollectionSearch(EntityManager entityManager) {
TestEntity testEntity = createTestEntity(1, 3);

testEntity.setName("CollectionSearchEntity");

entityManager.persist(testEntity);

return testEntity;
}

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 588bc34

Please sign in to comment.