[英]Spring JPA Data Repository failed to create bean for interface that extends CrudRepository
[英]CrudRepository in Spring JPA Repository
我试图了解在 Spring Boot 中使用JPA Repository
。
我能够使用以下 DAO 执行列表操作
@Repository
public interface CountryManipulationDAO extends CrudRepository<CountryEntity, Long>{
@Query("Select a from CountryEntity a")
public List<CountryEntity> listCountries();
因为, CountryEntity
主键是char
。 我对在 DAO 类中使用Long
感到困惑
谢谢
spring-data 中的Repository
接口有两个泛型类型参数; 要管理的域类以及域类的 id 类型。
所以第二个类型参数代表主键的类型。
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S entity);
T findOne(ID primaryKey);
Iterable<T> findAll();
Long count();
void delete(T entity);
boolean exists(ID primaryKey);
}
当你调用一个不使用实体 id 的函数时,不会进行类型匹配,你也不会遇到问题。 比如你的情况。
另一方面,在使用像findOne(ID)
、 exists(ID)
、 delete(ID)
和findAll(Iterable<ID>)
等使用 id 的操作时,您会遇到问题。
有关存储库的更多信息,请查看此处的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.