Polishing.
Fixed import formatting. Removed (non-Javadoc) comment, since we don't use them anymore. Simplified some code and removed superfluous or wrong Javadoc in touched files. Formatting. See #2388 Original pull request #2449
This commit is contained in:
@@ -17,14 +17,34 @@ package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.criteria.*;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Parameter;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.ParameterExpression;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
@@ -307,7 +327,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
/**
|
||||
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
|
||||
* {@link EntityGraph} information.
|
||||
*
|
||||
*/
|
||||
protected QueryHints getQueryHints() {
|
||||
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
|
||||
@@ -537,10 +556,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
return query.setMaxResults(1).getResultList().size() == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.JpaSpecificationExecutor#exists(org.springframework.data.jpa.domain.Specification)
|
||||
*/
|
||||
@Override
|
||||
public boolean exists(Specification<T> spec) {
|
||||
|
||||
@@ -567,8 +582,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
|
||||
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort)
|
||||
.getResultList();
|
||||
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort).getResultList();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -32,9 +32,6 @@ public class UserSpecifications {
|
||||
|
||||
/**
|
||||
* A {@link Specification} to match on a {@link User}'s firstname.
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
public static Specification<User> userHasFirstname(final String firstname) {
|
||||
|
||||
@@ -43,9 +40,6 @@ public class UserSpecifications {
|
||||
|
||||
/**
|
||||
* A {@link Specification} to match on a {@link User}'s lastname.
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
public static Specification<User> userHasLastname(final String lastname) {
|
||||
|
||||
@@ -54,27 +48,16 @@ public class UserSpecifications {
|
||||
|
||||
/**
|
||||
* A {@link Specification} to do a like-match on a {@link User}'s firstname.
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
public static Specification<User> userHasFirstnameLike(final String expression) {
|
||||
|
||||
return new Specification<User>() {
|
||||
|
||||
@Override
|
||||
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
||||
|
||||
return cb.like(root.get("firstname").as(String.class), String.format("%%%s%%", expression));
|
||||
}
|
||||
};
|
||||
return (root, query, cb) -> cb.like(root.get("firstname").as(String.class), String.format("%%%s%%", expression));
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link Specification} to do an age check.
|
||||
*
|
||||
* @param age upper (exclusive) bound of the age
|
||||
* @return
|
||||
*/
|
||||
public static Specification<User> userHasAgeLess(final Integer age) {
|
||||
|
||||
@@ -84,33 +67,19 @@ public class UserSpecifications {
|
||||
/**
|
||||
* A {@link Specification} to do a like-match on a {@link User}'s lastname but also adding a sort order on the
|
||||
* firstname.
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
public static Specification<User> userHasLastnameLikeWithSort(final String expression) {
|
||||
|
||||
return new Specification<User>() {
|
||||
return (root, query, cb) -> {
|
||||
|
||||
@Override
|
||||
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
||||
query.orderBy(cb.asc(root.get("firstname")));
|
||||
|
||||
query.orderBy(cb.asc(root.get("firstname")));
|
||||
|
||||
return cb.like(root.get("lastname").as(String.class), String.format("%%%s%%", expression));
|
||||
}
|
||||
return cb.like(root.get("lastname").as(String.class), String.format("%%%s%%", expression));
|
||||
};
|
||||
}
|
||||
|
||||
private static <T> Specification<T> simplePropertySpec(final String property, final Object value) {
|
||||
|
||||
return new Specification<T>() {
|
||||
|
||||
@Override
|
||||
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
|
||||
return builder.equal(root.get(property), value);
|
||||
}
|
||||
};
|
||||
return (root, query, builder) -> builder.equal(root.get(property), value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2643,6 +2643,7 @@ public class UserRepositoryTests {
|
||||
|
||||
@Test // GH-2388
|
||||
void existsWithSpec() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
Specification<User> minorSpec = userHasAgeLess(18);
|
||||
|
||||
Reference in New Issue
Block a user