【发布时间】:2017-12-05 08:11:59
【问题描述】:
我有一个实体:
@Entity
class Person {
@Id
@GeneratedValue
private Long id;
private String firstname, lastname;
private int age;
public Person() {
}
//getters and setters
还有一个投影界面:
public interface NamesOnly {
String getFirstname();
String getLastname();
}
投影中的方法名称与实体类中的getter完全相同。
我的仓库:
public interface PersonRepository extends JpaRepository<Person, Long> {
List<NamesOnly> findAll();
}
我无法编译它。
test/projectiontest/PersonRepository.java:[20,21] findAll() in test.projectiontest.PersonRepository clashes with findAll() in org.springframework.data.jpa.repository.JpaRepository
return type java.util.List<test.projectiontest.projection.NamesOnly> is not compatible with java.util.List<test.projectiontest.Person>
https://docs.spring.io/spring-data/jpa/docs/2.0.2.RELEASE/reference/html/#projections
我做错了什么?
【问题讨论】:
-
您正在使用
findAll,它已经在CrudRepository中定义了一个特定的签名。你的不符合那个。你需要另一个方法名。 -
@M.Deinum ok,例如
List<NamesOnly> findById(Long id);工作正常,但是如何使用投影接口实现findAll? -
定义自定义方法...