gpt4 book ai didi

java - Spring JPA 存储库中的 CrudRepository

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:50 25 4
gpt4 key购买 nike

我试图了解 JPA Repository 在 Spring Boot 中的使用。

我能够使用以下 DAO 执行列表操作

@Repository
public interface CountryManipulationDAO extends CrudRepository<CountryEntity, Long>{

@Query("Select a from CountryEntity a")
public List<CountryEntity> listCountries();

因为 CountryEntity 的主键是 char。我对在 DAO 类中使用 Long 感到困惑

谢谢

最佳答案

Repository spring-data 中的接口(interface)采用两个通用类型参数;要管理的域类以及域类的 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) 这样的 id 的操作时,您会遇到问题。 , exists(ID) , delete(ID)findAll(Iterable<ID>) .

有关存储库的更多信息,请查看文档 here .

关于java - Spring JPA 存储库中的 CrudRepository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com