「Java筆記」 SpringDataJpa 中 findOne() 方法報錯問題

「Java筆記」 SpringDataJpa 中 findOne() 方法報錯問題

資源介紹參數
資源類別: Java
如遇問題: 聯繫客服/留言反饋
釋放雙眼,帶上耳機,聽聽看~!

java-learning-notes

原文地址:https://www.miaoroom.com/code/note/springdatajpa-findone-bug.html
出處:喵容

首先我說一下我遇到問題的由來,視頻中用的是SpringDataJPA的1.11版本,可以使用findOne()方法根據id查詢,然後我使用了2.0.5版本,發現findOne()方法報錯了,不能用來當作根據id查詢了。

下面是報錯的代碼:

package com.miaoroom.sell.repository;

import com.miaoroom.sell.dataobject.ProductCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

/**
 * @Description: TODO
 * @create: 2019/1/19 14:51
 * @author: znnnnn
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
    @Autowired
    private ProductCategoryRepository repository;

    @Test
    public void findOneTest() {
        //ProductCategory productCategory = repository.findOne(1);//這一行報錯
        ProductCategory productCategory = repository.findById(1).get();
        System.out.println(productCategory.toString());

    }
}

2.0.5的已經變成了findById(id).get()來查詢了。這是兩個不同的版本,源碼已經發生變化。後來去找源碼中的findOne方法發現,findOne方法已經變了。想了解跟多的朋友可以去https://projects.spring.io/spring-boot/了解。

1.xx版本的CrudRepository類是這樣的:

@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    <S extends T> S save(S var1);

    <S extends T> Iterable<S> save(Iterable<S> var1);

    T findOne(ID var1);

    boolean exists(ID var1);

    Iterable<T> findAll();

    Iterable<T> findAll(Iterable<ID> var1);

    long count();

    void delete(ID var1);

    void delete(T var1);

    void delete(Iterable<? extends T> var1);

    void deleteAll();
}

2.x.x版本的CrudRepository類是這樣的:

@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
    <S extends T> S save(S var1);

    <S extends T> Iterable<S> saveAll(Iterable<S> var1);

    Optional<T> findById(ID var1);

    boolean existsById(ID var1);

    Iterable<T> findAll();

    Iterable<T> findAllById(Iterable<ID> var1);

    long count();

    void deleteById(ID var1);

    void delete(T var1);

    void deleteAll(Iterable<? extends T> var1);

    void deleteAll();
}

發現了嗎?findOne方法不在CrudRepository中了

而現在的findOne去了哪裡呢?

public interface QueryByExampleExecutor<T> {
    <S extends T> Optional<S> findOne(Example<S> var1);

    <S extends T> Iterable<S> findAll(Example<S> var1);

    <S extends T> Iterable<S> findAll(Example<S> var1, Sort var2);

    <S extends T> Page<S> findAll(Example<S> var1, Pageable var2);

    <S extends T> long count(Example<S> var1);

    <S extends T> boolean exists(Example<S> var1);
}
聲明:本文為原創作品,版權歸作者所有。未經許可,不得轉載或用於任何商業用途。如若本站內容侵犯了原著者的合法權益,可聯繫我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
2 條回復 A文章作者 M管理員
  1. baby song

    這個問題最近碰到好多次了,啊哈哈

    • 喵容站長

      那來這寫寫筆記啊[wb_doge]現在流量漲得挺快