jpa2.0以上findOne和getOne的區別


 
/**
 * Returns a single entity matching the given {@link Example} or {@literal null} if none was found.
 *
 * @param example must not be {@literal null}.
 * @return a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found.
 * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result.
 */
<S extends T> Optional<S> findOne(Example<S> example);
 
 
/**
 * Returns a reference to the entity with the given identifier.
 *
 * @param id must not be {@literal null}.
 * @return a reference to the entity with the given identifier.
 * @see EntityManager#getReference(Class, Object)
 * @throws javax.persistence.EntityNotFoundException if no entity exists for given {@code id}.
 */
T getOne(ID id);

 

findone:返回實體的optional對象

getone:返回實體的引用,代理對象

 

實體轉json的過程中會出錯,用debug查看實體其實是空,數據是放在代理對象中的,但jackson將實體轉json沒有拿到代理對象,然后產生以下錯誤,這時候json轉換失敗再導致輸出流報錯。

 

getOne示例:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM