1. 程式人生 > >jpa2.0以上findOne和getOne的區別

jpa2.0以上findOne和getOne的區別

exceptio 報錯 EDA man vax ram fin ava 轉換

/**
 * 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示例:

技術分享圖片

jpa2.0以上findOne和getOne的區別