Commit cdbb5f44 authored by Matija Obreza's avatar Matija Obreza

Querydsl does not allow `null` predicate for `findAll()`

parent 1c924ed7
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
<version>1.1.3</version> <version>1.1.3</version>
<executions> <executions>
<execution> <execution>
<id>generate-source-entities</id>
<phase>generate-sources</phase>
<goals> <goals>
<goal>process</goal> <goal>process</goal>
</goals> </goals>
...@@ -51,6 +53,21 @@ ...@@ -51,6 +53,21 @@
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>generate-test-entities</id>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<includes>
<!-- List packages to be processed -->
<include>org.genesys.blocks.model</include>
</includes>
<outputDirectory>target/generated-test-sources/querydsl</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions> </executions>
<dependencies> <dependencies>
<dependency> <dependency>
......
...@@ -39,6 +39,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; ...@@ -39,6 +39,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.ExpressionUtils; import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.CollectionPathBase; import com.querydsl.core.types.dsl.CollectionPathBase;
...@@ -84,7 +85,8 @@ public abstract class SuperModelFilter<T extends SuperModelFilter<T, R>, R> { ...@@ -84,7 +85,8 @@ public abstract class SuperModelFilter<T extends SuperModelFilter<T, R>, R> {
* @return the predicate * @return the predicate
*/ */
public Predicate buildPredicate() { public Predicate buildPredicate() {
return ExpressionUtils.allOf(collectPredicates()); Predicate collected = ExpressionUtils.allOf(collectPredicates());
return collected == null ? new BooleanBuilder() : collected;
} }
/** /**
......
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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
*
* http://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 org.genesys.blocks.model;
import java.util.List;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.VersionedModelFilter;
import com.querydsl.core.types.Predicate;
public class VersionedEntityFilter extends VersionedModelFilter<VersionedEntityFilter, VersionedEntity> {
public StringFilter name;
public List<Predicate> collectPredicates() {
return collectPredicates(QVersionedEntity.versionedEntity);
}
public List<Predicate> collectPredicates(QVersionedEntity versionedEntity) {
final List<Predicate> predicates = super.collectPredicates(versionedEntity, versionedEntity._super._super);
if (name != null) {
predicates.add(name.buildQuery(versionedEntity.name));
}
return predicates;
}
}
...@@ -17,10 +17,11 @@ package org.genesys.blocks.persistence; ...@@ -17,10 +17,11 @@ package org.genesys.blocks.persistence;
import org.genesys.blocks.model.VersionedEntity; import org.genesys.blocks.model.VersionedEntity;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
/** /**
* The Interface VersionedEntityRepository. * The Interface VersionedEntityRepository.
*/ */
public interface VersionedEntityRepository extends JpaRepository<VersionedEntity, Long> { public interface VersionedEntityRepository extends JpaRepository<VersionedEntity, Long>, QuerydslPredicateExecutor<VersionedEntity> {
} }
...@@ -15,14 +15,15 @@ ...@@ -15,14 +15,15 @@
*/ */
package org.genesys.blocks.tests.model; package org.genesys.blocks.tests.model;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is; import static org.junit.Assert.*;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.model.VersionedEntity; import org.genesys.blocks.model.VersionedEntity;
import org.genesys.blocks.model.VersionedEntityFilter;
import org.genesys.blocks.tests.ServiceTest; import org.genesys.blocks.tests.ServiceTest;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
...@@ -75,6 +76,26 @@ public class VersionedModelTest extends ServiceTest { ...@@ -75,6 +76,26 @@ public class VersionedModelTest extends ServiceTest {
assertThat(entity.isActive(), is(false)); assertThat(entity.isActive(), is(false));
} }
@Test
public void testEmptyFilter() {
VersionedEntityFilter filter = new VersionedEntityFilter();
Page<VersionedEntity> page = versionedEntityRepository.findAll(filter.buildPredicate(), PageRequest.of(0, 10));
assertThat(page, not(nullValue()));
}
@Test
public void testStringFilter() {
VersionedEntity entity = versionedEntityRepository.save(createEntity());
VersionedEntityFilter filter = new VersionedEntityFilter();
filter.id().add(entity.getId());
Page<VersionedEntity> page = versionedEntityRepository.findAll(filter.buildPredicate(), PageRequest.of(0, 10));
assertThat(page, not(nullValue()));
assertThat(page.getContent().size(), is(1));
assertThat(page.getContent().get(0), is(entity));
}
/** /**
* Creates the entity. * Creates the entity.
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment