【发布时间】:2017-10-13 13:06:18
【问题描述】:
我正在尝试通过此实现在 Spring Data JPA 中使用规范来实现投影:
https://github.com/pramoth/specification-with-projection
相关类如下:
规格:
public class TopicSpec {
public static Specification<Topic> idEq(String id){
return (root, query, cb) -> cb.equal(root.get(Topic_.id),id);
}
}
存储库
@Repository
public interface TopicRepository extends JpaRepository<Topic,String>,JpaSpecificationExecutorWithProjection<Topic> {
public static interface TopicSimple{
String getId();
String getName();
}
List<TopicSimple> findById(String id);
}
测试
@Test
public void specificationWithProjection() {
Specification<Topic> where= Specifications.where(TopicSpec.idEq("Bir"));
List<Topic> all = topicRepository.findAll(where);
Assertions.assertThat(all).isNotEmpty();
}
我从 Get 方法得到这个响应:
但是测试失败了。此外,当我拉取 pramoth 的 github 项目时,我可以成功运行测试。有人对这个问题有意见吗?
完整的项目可以在这里找到: https://github.com/dengizik/projectionDemo
【问题讨论】:
标签: spring-data-jpa projection specifications