注意getOne是“Returns a reference“ findOne比getOne更通用。推荐使用findOne。具体原因如下: it's just that/** * Retrieves an entity by its id. * * @param id must not be {@literal null}. * @return the entity with the given id or {@literal null} if none found * @throws IllegalArgumentException if {@code id} is {@literal null} */ T findOne(ID id); /** * 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) */ T getOne(ID id);
findOne(ID)
is more generic than the more specialisedgetOne(ID)
. Which one you use is up to you and your project but I would personally stick to thefindOne(ID)
as it makes your code less implementation specific and opens the doors to move to things like MongoDB etc. in the future without too much refactoring :) 欢迎关注橙子博客微信公众号:chengziboke888
查看原文:http://zccbbg.top/2017/05/24/111-2/
jpa findone 和 getone的区别
最新推荐文章于 2024-07-06 19:47:31 发布