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

原文地址:https://www.cnblogs.com/yinwutuan/p/9516167.html