Skip to content

Commit

Permalink
[Wen Hao] - support spring-data-jpa version 2.0.5.RELEASE.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenhao committed Mar 16, 2018
1 parent 458cfdf commit fdfa684
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ English Version:

[Latest]

[3.2.0]
[3.2.1]

[3.1.0]

Expand All @@ -31,7 +31,7 @@ Chinese Version:

[最新]

[3.2.0_cn]
[3.2.1_cn]

[3.1.0_cn]

Expand All @@ -45,7 +45,7 @@ repositories {
}
dependencies {
compile 'com.github.wenhao:jpa-spec:3.2.0'
compile 'com.github.wenhao:jpa-spec:3.2.1'
}
```

Expand All @@ -55,7 +55,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand Down Expand Up @@ -380,11 +380,11 @@ Licensed under [Apache License]


[Latest]: ./docs/3.2.1.md
[3.2.0]: ./docs/3.2.0.md
[3.2.1]: ./docs/3.2.1.md
[3.1.0]: ./docs/3.1.0.md
[3.0.0]: ./docs/3.0.0.md
[最新]: ./docs/3.2.0_cn.md
[3.2.0_cn]: ./docs/3.2.0_cn.md
[最新]: ./docs/3.2.1_cn.md
[3.2.1_cn]: ./docs/3.2.1_cn.md
[3.1.0_cn]: ./docs/3.1.0_cn.md
[3.0.0_cn]: ./docs/3.0.0_cn.md
[Legacy Hibernate Criteria Queries]: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#appendix-legacy-criteria
Expand Down
6 changes: 3 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repositories {
}
dependencies {
compile 'com.github.wenhao:jpa-spec:3.2.0'
compile 'com.github.wenhao:jpa-spec:3.2.1'
}
```

Expand All @@ -49,7 +49,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand All @@ -59,7 +59,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
}

group = 'com.github.wenhao'
version = '3.2.0'
version = '3.2.1'

idea {
project {
Expand Down Expand Up @@ -119,7 +119,7 @@ publishing {
artifact javadocJar
groupId 'com.github.wenhao'
artifactId 'jpa-spec'
version '3.2.0'
version '3.2.1'
pom.withXml {
def root = asNode()
root.appendNode('description', 'A JAP Query By Specification framework.')
Expand Down Expand Up @@ -154,10 +154,10 @@ bintray {
githubRepo = 'wenhao/jpa-spec'
githubReleaseNotesFile = 'README.md'
version {
name = '3.2.0'
desc = 'A JAP Query By Specification framework 3.2.0'
name = '3.2.1'
desc = 'A JAP Query By Specification framework 3.2.1'
released = new Date()
vcsTag = '3.2.0'
vcsTag = '3.2.1'
gpg {
sign = true
passphrase = 'passphrase'
Expand Down
4 changes: 2 additions & 2 deletions docs/3.2.0.md → docs/3.2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
}
dependencies {
compile 'com.github.wenhao:jpa-spec:3.2.0'
compile 'com.github.wenhao:jpa-spec:3.2.1'
}
```

Expand All @@ -20,7 +20,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand Down
6 changes: 3 additions & 3 deletions docs/3.2.0_cn.md → docs/3.2.1_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
}
dependencies {
compile 'com.github.wenhao:jpa-spec:3.2.0'
compile 'com.github.wenhao:jpa-spec:3.2.1'
}
```

Expand All @@ -20,7 +20,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand All @@ -30,7 +30,7 @@ dependencies {
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/github/wenhao/jpa/PredicateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static javax.persistence.criteria.Predicate.BooleanOperator.OR;

Expand Down Expand Up @@ -131,11 +135,14 @@ public PredicateBuilder<T> predicate(boolean condition, Specification specificat
}

public Specification<T> build() {
return (root, query, cb) -> {
return (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
Predicate[] predicates = new Predicate[specifications.size()];
for (int i = 0; i < specifications.size(); i++) {
predicates[i] = specifications.get(i).toPredicate(root, query, cb);
}
if (Objects.equals(predicates.length, 0)) {
return null;
}
return OR.equals(operator) ? cb.or(predicates) : cb.and(predicates);
};
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/github/wenhao/jpa/integration/OrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Date;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.domain.Range.Bound.inclusive;

Expand Down Expand Up @@ -58,4 +60,30 @@ public void should_be_able_to_find_by_using_or_with_multiple_values() {
// then
assertThat(persons.size()).isEqualTo(3);
}

@Test
public void should_be_able_to_find_all_if_all_predicate_are_null() {
// given
Person jack = new PersonBuilder()
.name("Jack")
.age(18)
.build();
Person eric = new PersonBuilder()
.name("Eric")
.age(20)
.build();
personRepository.save(jack);
personRepository.save(eric);

// when
Specification<Person> specification = Specifications.<Person>or()
.eq(isNotBlank(EMPTY), "name", jack.getName())
.like(isNotBlank(EMPTY), "name", "%" + jack.getName() + "%")
.build();

List<Person> persons = personRepository.findAll(specification);

// then
assertThat(persons.size()).isEqualTo(2);
}
}

0 comments on commit fdfa684

Please sign in to comment.