欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JPA findById方法和getOne方法的區(qū)別說明

 更新時間:2021年08月14日 14:14:21   作者:Breeze丶4379  
這篇文章主要介紹了JPA findById方法和getOne方法的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。

findById方法和getOne方法區(qū)別

Jpa基礎(chǔ)的CRUD方法繼承自接口CrudRepository<T, ID>,包含以下方法:

<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();

getOne()方法是JpaRepository接口中定義的

源碼如下:

/**
 * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
 * implemented this is very likely to always return an instance and throw an
 * {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
 * immediately.
 *
 * @param id must not be {@literal null}.
 * @return a reference to the entity with the given identifier.
 * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
 */
T getOne(ID id);

網(wǎng)上找的源碼:

public T getOne(ID id) {
    Assert.notNull(id, "The given id must not be null!");
    return this.em.getReference(this.getDomainClass(), id);
}

由getOne方法的源碼可見,getOne方法會返回一個和給定id匹配的實體的引用,方法實際是調(diào)用getReference方法,也就是說加載策略是延遲加載,直到調(diào)用這個對象的時候才真正從數(shù)據(jù)庫中進行查詢(x_x)。

再看findById()方法

public Optional<T> findById(ID id) {
    Assert.notNull(id, "The given id must not be null!");
    Class<T> domainType = this.getDomainClass();
    if (this.metadata == null) {
        return Optional.ofNullable(this.em.find(domainType, id));
    } else {
        LockModeType type = this.metadata.getLockModeType();
        Map<String, Object> hints = this.getQueryHints().withFetchGraphs(this.em).asMap();
        return Optional.ofNullable(type == null ? this.em.find(domainType, id, hints) : this.em.find(domainType, id, type, hints));
    }
}

findById實際上調(diào)用的是find方法,采用立即加載方式,執(zhí)行這條查詢語句的時候就會立刻從數(shù)據(jù)庫中進行查詢,不過findById方法返回的是一個Optional,需要對這個Optional調(diào)用.get()方法才能得到需要的實體。

總結(jié)就是getOne方法是懶加載,直到調(diào)用它返回的實體時才會對數(shù)據(jù)庫進行查詢,findById是立即加載,主要調(diào)用方法就會去數(shù)據(jù)庫查詢。getOne方法直接返回一個實體,findById方法返回一個Optional,需要調(diào)用.get()方法獲取實體。

spring-data-jpa中findById()的使用

springboot 2.x 版本后,較之前的版本在此方法的使用上有差:

如果找到匹配的id數(shù)據(jù),則賦值給foo;否則則將括號中的對象賦值給foo。

Foo foo = repository.findById(id)
                    .orElse(null);
Foo foo = repository.findById(id)
                    .orElse(new Object());

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論