我正在尝试在我的存储库findAll方法中使用规范,但我收到此消息:
Cannot resolve method 'findAll(org.springframework.data.jpa.domain.Specification<com.package.business.Eleve>)'
这是我的规范类:
public class EleveSpecification {
public static Specification<Eleve> nomIsLike(final String nom) {
return new Specification<Eleve>() {
@Override
public Predicate toPredicate(Root<Eleve> personRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
String likePattern = getLikePattern(nom);
return cb.like(cb.lower(personRoot.<String>get(Eleve_.nom)), likePattern);
}
private String getLikePattern(final String nom) {
StringBuilder pattern = new StringBuilder();
pattern.append(nom.toLowerCase());
pattern.append("%");
return pattern.toString();
}
};
}
}
我使用JUnit来测试我的规范:
@org.junit.Test
public void findByNom() {
List<Eleve> eleves = eleveRepository.findAll(EleveSpecification.nomIsLike("yassine"));
//System.out.print(eleve.getNom());
for (Eleve eleve : eleves) {
System.out.print("" + eleve.getNom() + " " + eleve.getPrenom());
}
}
这是我的存储库界面:
public interface EleveRepository extends JpaRepository<Eleve, Integer> {
}
答案 0 :(得分:3)
您的存储库必须是:
public interface EleveRepository extends JpaRepository<Eleve, Integer>, JpaSpecificationExecutor<Eleve> {
}