亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PersonRepository 類型未定義方法 findOne(Long)

PersonRepository 類型未定義方法 findOne(Long)

慕的地6264312 2023-04-19 16:47:40
我正在嘗試使用 Spring boot 構建簡單的博客應用程序。但是,當我嘗試將 findOne 用于我的 service.java 時,現在我遇到了“方法 findOne(Long) 對于類型 PersonRepository 未定義”的問題。下面是什么我做了我試圖在存儲庫中創建對象,指示 findOne 并保存。但是它沒有幫助PersonRepository.javapackage PersonRepository.javaio.javabrains.repository;import org.springframework.data.repository.CrudRepository;import org.springframework.stereotype.Repository;import io.javabrains.Entity.Person;@Repositorypublic interface PersonRepository extends CrudRepository<Person,Long>{public Person findByEmail(String email);    /*     * public Person findOne(Long id);     *      * public Iterable<Person> findAll();     *      * public Person save(Person person);     */PersonService.java@Servicepublic class PersonService {    @Autowired    private PersonRepository personRepository;    public Object findAll(){        return personRepository.findAll();    }    public Person findById(Long id){        return personRepository.findOne(id);    }我希望消除評論塊可以解決問題。但是,當我嘗試運行該應用程序時,它顯示錯誤
查看完整描述

2 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

最好使用JpaRepository (它擴展了 CRUD 存儲庫)

而不是findOne()你可以使用getOne()(可選)findById()

findById()

通過其 id 檢索實體。

參數:id 不能為空。

返回:具有給定 id 的實體或 Optional#empty() 如果沒有找到

拋出:IllegalArgumentException - 如果 id 為 null。

得到一個()

返回對具有給定標識符的實體的引用。根據 JPA 持久性提供程序的實現方式,這很可能總是返回一個實例并在首次訪問時拋出 javax.persistence.EntityNotFoundException。其中一些會立即拒絕無效標識符。

參數:id 不能為空。

返回:對具有給定標識符的實體的引用。


查看完整回答
反對 回復 2023-04-19
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

請檢查您使用的 Spring 版本。可能是你的findOne被替換了findById。

因此,您的存儲庫將變為:

public Person findByEmail(String email);
     public Person findById(Long id);


查看完整回答
反對 回復 2023-04-19
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號