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)文章
SpringMVC @RequestMapping注解應用方法示例講解
通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09使用IDEA創(chuàng)建Java Web項目并部署訪問的圖文教程
本文通過圖文并茂的形式給大家介紹了使用IDEA創(chuàng)建Java Web項目并部署訪問的教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08Java overload和override的區(qū)別分析
方法的重寫(Overriding)和重載(Overloading)是Java多態(tài)性的不同表現(xiàn),想要了解更多請參考本文2012-11-11springcloud項目里application.yml不加載的坑及解決
這篇文章主要介紹了springcloud項目里application.yml不加載的坑及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07springboot整合mybatis將sql打印到日志的實例詳解
這篇文章主要介紹了springboot整合mybatis將sql打印到日志的實例詳解,需要的朋友可以參考下2017-12-12Java?SE封裝、包、static關(guān)鍵字和代碼塊示例詳解
這篇文章主要給大家介紹了關(guān)于Java?SE封裝、包、static關(guān)鍵字和代碼塊的相關(guān)資料,需要的朋友可以參考下2023-11-11springboot-jta-atomikos多數(shù)據(jù)源事務管理實現(xiàn)
本文主要介紹了springboot-jta-atomikos多數(shù)據(jù)源事務管理實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03