簡體   English   中英

Spring JPA 存儲庫中的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM