我无法同时使用Spring Data JPA预测和规范。我有以下设置:
实体:
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "CODE", nullable = false)
private String code;
---getters & setters---
}
投影界面:
public interface CountryProjection {
String getName();
}
国家/地区规格:
public class CountrySpecification {
public static Specification<Country> predicateName(final String name) {
return new Specification<Country>() {
@Override
public Predicate toPredicate(Root<Country> eventRoot, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(eventRoot.get(Country_.name), name);
}
};
}
}
存储库:
public interface CountryRepository extends JpaRepository<Country, Long>, JpaSpecificationExecutor<Country> {
List<CountryProjection> findByName(String name); // works fine
List<CountryProjection> findAllProjectedBy(); // works fine
List<CountryProjection> findAllProjectedBy(Specification<Country> specification); //throws Exception as shown below
}
前两个方法findByName和findAllProjectedBy可以正常工作。
而第三个方法findAllProjectedBy(Specification Specification)会引发以下异常-
如何做到这一点?有任何想法吗?
最佳答案
尚不支持混合投影和规格。有一个bug对此进行跟踪。