英文:
Is Persistence.getPersistenceUtil a simpler way of testing whether a lazy loaded JPA collection is initialized when using Spring Boot repositories
问题
这个问题在这里有答案 - https://stackoverflow.com/questions/4306463/how-to-test-whether-lazy-loaded-jpa-collection-is-initialized
在那里给出的答案使用了 PersistenceUnitUtil
。然而,为了获得一个 PersistenceUnitUtil,您需要访问 EntityManagerFactory。Spring 框架使用 repositories 来隐藏所有的 EntityManager 相关内容,这使得该解决方案变得尴尬(但不是不可能)。
然而,在标准的 JPA 中有一种访问类似对象 PersistenceUtil
的方式 - 就像是 Persistence.getPersistenceUtil
- 它提供了一个具有与上面答案中使用的相同方法的对象。
在 Spring 框架中,使用 repositories,这似乎是一个更简单的访问那些有用的 isLoaded
方法的方式。
是否有任何理由不使用这种更简单的方法?
英文:
This question is given an answer here - https://stackoverflow.com/questions/4306463/how-to-test-whether-lazy-loaded-jpa-collection-is-initialized
The answer given there makes use of PersistenceUnitUtil
. However, in order to get a PersistenceUnitUtil you need to access the EntityManagerFactory. Spring framework uses repositories to hide all that EntityManager stuff, which makes that solution awkward (but not impossible).
However there is a standard JPA way of accessing a similar object PersistenceUtil
- just as Persistence.getPersistenceUtil
- which provides an object with the same methods used in the above answer.
In the Spring framework, using repositories, this appears to be a much simpler way to access those helpful isLoaded
methods.
Is there is any reason not to use that simpler approach?
专注分享java语言的经验与见解,让所有开发者获益!
评论