Skip to content

Commit ed7df2e

Browse files
committed
DATAJPA-218 - Polishing.
Adapted to API changes in Spring Data Commons. Related tickets: DATACMNS-810. Original pull request: #164.
1 parent 75f2719 commit ed7df2e

File tree

4 files changed

+58
-73
lines changed

4 files changed

+58
-73
lines changed

src/main/asciidoc/query-by-example.adoc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ In Spring Data JPA you can use Query by Example with Repositories.
77
====
88
[source, java]
99
----
10-
public interface PersonRepository extends JpaRepository<Person, String> {
11-
12-
}
10+
public interface PersonRepository extends JpaRepository<Person, String> { … }
1311
1412
public class PersonService {
1513
@@ -22,10 +20,7 @@ public class PersonService {
2220
----
2321
====
2422

25-
An `Example` containing an untyped `ExampleSpec` uses the Repository type. Typed `ExampleSpec` use their type for creating JPA queries.
26-
27-
NOTE: Only SingularAttribute properties can be used for property matching.
28-
23+
NOTE: Only `SingularAttribute` properties can currently be used for property matching.
2924

3025
Property specifier accepts property names (e.g. "firstname" and "lastname"). You can navigate by chaining properties together with dots ("address.city"). You can tune it with matching options and case sensitivity.
3126

src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
import org.springframework.dao.InvalidDataAccessApiUsageException;
3636
import org.springframework.data.domain.Example;
37-
import org.springframework.data.domain.ExampleSpec;
38-
import org.springframework.data.repository.core.support.ExampleSpecAccessor;
37+
import org.springframework.data.domain.ExampleMatcher;
38+
import org.springframework.data.repository.core.support.ExampleMatcherAccessor;
3939
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;
4040
import org.springframework.orm.jpa.JpaSystemException;
4141
import org.springframework.util.Assert;
@@ -78,7 +78,7 @@ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Examp
7878
Assert.notNull(example, "Example must not be null!");
7979

8080
List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(),
81-
example.getProbeType(), new ExampleSpecAccessor(example.getExampleSpec()),
81+
example.getProbeType(), new ExampleMatcherAccessor(example.getMatcher()),
8282
new PathNode("root", null, example.getProbe()));
8383

8484
if (predicates.isEmpty()) {
@@ -94,7 +94,7 @@ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Examp
9494

9595
@SuppressWarnings({ "rawtypes", "unchecked" })
9696
static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> from, ManagedType<?> type, Object value,
97-
Class<?> probeType, ExampleSpecAccessor exampleAccessor, PathNode currentNode) {
97+
Class<?> probeType, ExampleMatcherAccessor exampleAccessor, PathNode currentNode) {
9898

9999
List<Predicate> predicates = new ArrayList<Predicate>();
100100
DirectFieldAccessFallbackBeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(value);
@@ -112,7 +112,7 @@ static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> fr
112112

113113
if (attributeValue == null) {
114114

115-
if (exampleAccessor.getNullHandler().equals(ExampleSpec.NullHandler.INCLUDE)) {
115+
if (exampleAccessor.getNullHandler().equals(ExampleMatcher.NullHandler.INCLUDE)) {
116116
predicates.add(cb.isNull(from.get(attribute)));
117117
}
118118
continue;

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.springframework.data.domain.PageImpl;
4545
import org.springframework.data.domain.Pageable;
4646
import org.springframework.data.domain.Sort;
47-
import org.springframework.data.domain.TypedExampleSpec;
4847
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
4948
import org.springframework.data.jpa.domain.Specification;
5049
import org.springframework.data.jpa.provider.PersistenceProvider;
@@ -148,8 +147,8 @@ public void delete(ID id) {
148147
T entity = findOne(id);
149148

150149
if (entity == null) {
151-
throw new EmptyResultDataAccessException(String.format("No %s entity with id %s exists!",
152-
entityInformation.getJavaType(), id), 1);
150+
throw new EmptyResultDataAccessException(
151+
String.format("No %s entity with id %s exists!", entityInformation.getJavaType(), id), 1);
153152
}
154153

155154
delete(entity);
@@ -422,7 +421,7 @@ public List<T> findAll(Specification<T> spec, Sort sort) {
422421
@Override
423422
public <S extends T> S findOne(Example<S> example) {
424423
try {
425-
return getQuery(new ExampleSpecification<S>(example), getResultType(example), (Sort) null).getSingleResult();
424+
return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getSingleResult();
426425
} catch (NoResultException e) {
427426
return null;
428427
}
@@ -431,18 +430,18 @@ public <S extends T> S findOne(Example<S> example) {
431430
/* (non-Javadoc)
432431
* @see org.springframework.data.repository.query.QueryByExampleExecutor#count(org.springframework.data.domain.Example)
433432
*/
434-
@SuppressWarnings("unchecked")
435433
@Override
434+
@SuppressWarnings("unchecked")
436435
public <S extends T> long count(Example<S> example) {
437-
return executeCountQuery(getCountQuery(new ExampleSpecification<S>(example), getResultType(example)));
436+
return executeCountQuery(getCountQuery(new ExampleSpecification<S>(example), example.getProbeType()));
438437
}
439438

440439
/* (non-Javadoc)
441440
* @see org.springframework.data.repository.query.QueryByExampleExecutor#exists(org.springframework.data.domain.Example)
442441
*/
443442
@Override
444443
public <S extends T> boolean exists(Example<S> example) {
445-
return !getQuery(new ExampleSpecification<S>(example), getResultType(example), (Sort) null).getResultList()
444+
return !getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getResultList()
446445
.isEmpty();
447446
}
448447

@@ -452,7 +451,7 @@ public <S extends T> boolean exists(Example<S> example) {
452451
*/
453452
@Override
454453
public <S extends T> List<S> findAll(Example<S> example) {
455-
return getQuery(new ExampleSpecification<S>(example), getResultType(example), (Sort) null).getResultList();
454+
return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getResultList();
456455
}
457456

458457
/*
@@ -461,7 +460,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
461460
*/
462461
@Override
463462
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
464-
return getQuery(new ExampleSpecification<S>(example), getResultType(example), sort).getResultList();
463+
return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), sort).getResultList();
465464
}
466465

467466
/*
@@ -472,9 +471,10 @@ public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
472471
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
473472

474473
ExampleSpecification<S> spec = new ExampleSpecification<S>(example);
475-
TypedQuery<S> query = getQuery(new ExampleSpecification<S>(example), getResultType(example), pageable);
476-
return pageable == null ? new PageImpl<S>(query.getResultList())
477-
: readPage(query, getResultType(example), pageable, spec);
474+
Class<S> probeType = example.getProbeType();
475+
TypedQuery<S> query = getQuery(new ExampleSpecification<S>(example), probeType, pageable);
476+
477+
return pageable == null ? new PageImpl<S>(query.getResultList()) : readPage(query, probeType, pageable, spec);
478478
}
479479

480480
/*
@@ -743,15 +743,6 @@ private void applyQueryHints(Query query) {
743743
}
744744
}
745745

746-
747-
private <S extends T> Class<S> getResultType(Example<S> example) {
748-
749-
if(example.getExampleSpec() instanceof TypedExampleSpec<?>){
750-
return example.getResultType();
751-
}
752-
return (Class<S>) getDomainClass();
753-
}
754-
755746
/**
756747
* Executes a count query and transparently sums up all values returned.
757748
*

0 commit comments

Comments
 (0)