编辑:元模型可能出现问题。当我将规范从userRoot.<String>get(User_.lastName)
更改为userRoot.<String>get("lastName")
时,它就可以了。
我正在尝试按lastName过滤用户。 我创建了元模型类:
@StaticMetamodel(User.class)
public class User_ {
public static volatile SingularAttribute<User, String> lastName;
}
规格类:
public class UserSpecifications {
/**
* Creates a specification used to find users whose last name begins with
* the given search term. This search is case insensitive.
* @param searchTerm
* @return
*/
public static Specification<User> lastNameIsLike(final String searchTerm) {
return new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> userRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
String likePattern = getLikePattern(searchTerm);
return cb.like(cb.lower(userRoot.<String>get(User_.lastName)), likePattern);
}
private String getLikePattern(final String searchTerm) {
StringBuilder pattern = new StringBuilder();
pattern.append(searchTerm.toLowerCase());
pattern.append("%");
return pattern.toString();
}
};
}
}
userRepository实现JpaSpecificationExecutor<User>
接口
现在,我应该可以使用以下内容获取已过滤(和已分页)的用户:
userRepository.findAll(lastNameIsLike(keyword), pageRequest);
但它不起作用:(
[ERROR] my.namespace.aop.logging.LoggingAspect - Exception in my.namespace.web.rest.UserResource.getList() with cause = null
java.lang.NullPointerException: null
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:140) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at my.namespace.domain.specification.UserSpecifications$1.toPredicate(UserSpecifications.java:31) ~[classes/:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:538) ~[spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:489) ~[spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:474) ~[spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:356) ~[spring-data-jpa-1.7.1.RELEASE.jar:na]