在Spring数据实现与规格投影JPA
问题描述:
我想实现通过此实现与春季数据JPA规范的投影:在Spring数据实现与规格投影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();
}
我从一开始这个方法响应:
但是测试失败。除了当我拉动gratub项目的时候,我可以成功运行测试。有没有人对这个问题有任何意见?
整个项目可以在这里找到: https://github.com/dengizik/projectionDemo
答
我也问过同样的问题,以项目Pramoth Suwanpech,谁还跟来检查我的代码,并给出答案的开发商。我的测试类应该已经实现测试对象是这样的:
@Before
public void init() {
Topic topic = new Topic();
topic.setId("İki");
topic.setName("Hello");
topicRepository.save(topic); }
使用此设置通过测试。