代碼筆記

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

原文地址: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);
}

View Comments

Recent Posts

Flexible Shipping Pro

在WordPress的世界裡,…

4天 ago

2023 年 WordPress 中最棒的多語言翻譯外掛推薦

擔心如何翻譯您的網站語言以支持…

1年 ago

2023 年 WordPress 中最棒的可視化頁面構建器外掛推薦

在設計任何頁面或網站時,對於不…

1年 ago

Ella 多用途 Shopify 佈景主題

Shopify 佈景主題市場上有許…

1年 ago

AI Engine Pro

喵容今天帶來的 AI Engi…

1年 ago

AIKit

喵容今天為您帶來 AIKit …

1年 ago