JPA 2 - 在 JavaSE 中使用 EntityManager - 几个问题
我有几个关于在 JavaSE 环境中使用实体管理器的问题。
我正在使用存储库模式对数据库执行 CRUD 操作。将有一个像这样的基本存储库类:
public class Repository<T> implements IRepository<T> {
private EntityManager em;
private String persistenceUnitName;
public Repository(String persistenceUnitName) {
this.persistenceUnitName = persistenceUnitName;
}
@Override
public T find(Class<T> type, Object id) {
return em.find(type, id);
}
private EntityManager getEntityManager() {
if (this.em == null) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);
em = emf.createEntityManager();
}
return em;
}
...
...
}
然后我将拥有像 EmployeeRepository 这样的类,它将继承 Repository。这些存储库类将在我的服务层中创建。
这是初始化实体管理器的好方法吗?我开始认为这不是 - 似乎每个持久性单元应该只有一个实体管理器?在这种情况下,您创建的每个存储库都会有一个实体管理器...您将如何确保每个持久性单元只有一个实体管理器?另外,我注意到实体管理器和实体管理器工厂方法有一个 close 方法 - 何时应该调用它们?在服务器终止事件上?
如果您知道有关在 JavaSE 中使用 JPA 的任何好的资源,我将不胜感激。
谢谢!
I have a couple questions regarding using the entity manager in a JavaSE environment.
I'm using the repository pattern to perform my CRUD operations on the database. There will be a base repository class like so:
public class Repository<T> implements IRepository<T> {
private EntityManager em;
private String persistenceUnitName;
public Repository(String persistenceUnitName) {
this.persistenceUnitName = persistenceUnitName;
}
@Override
public T find(Class<T> type, Object id) {
return em.find(type, id);
}
private EntityManager getEntityManager() {
if (this.em == null) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);
em = emf.createEntityManager();
}
return em;
}
...
...
}
I will then have classes like EmployeeRepository that will inherit Repository. These repository classes will be created in my service layer.
Is this a good way of initializing the entity manager? I'm starting to think that it's not - it seems like you should only have one entity manager per persistence unit? Where as in this case you would have an entity manager for every repository you create... How would you go about ensuring you only have one entity manager per persistence unit? Also, I noticed that the entity manager and entity manager factory methods have a close method - when should these be called? On a server terminate event?
If you know of any good sources about using JPA in JavaSE I would appreciate the info.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎。每个持久性单元只需要一个 EntityManagerFactory。
通常开发人员使用单例 EntityManagerFactory 创建帮助器类,例如
EntityManager 用于与一组称为持久性上下文的托管实体实例进行交互。
如果你想知道为什么我使用 ErrorInInitializerError,它的API很明确
...
那么,服务层用于划定事务边界。因此,对于每个用例,您可以创建 EntityManager 并为每个协作者传递引用,以帮助您执行用例。
现在假设您需要上面显示的每个用例的样板代码。
您可以依靠 Spring 来帮助您摆脱这个样板代码。
Almost. You need just one EntityManagerFactory per persistence unit.
Usually developers create an helper class with a singleton EntityManagerFactory such as
EntityManager, in other hand, is used to interact with a set of managed entity instances called persistence context.
If you want to know why i use ErrorInInitializerError, its API is clear
...
Well, the service layer is used to delimit Transaction boundary. So for each use case, you can create your EntityManager and pass by reference for each colaborator needed to help you to execute your use case.
Now imagine you need the boilerplate code shown above for each use case.
You can rely on Spring to help you to get rid of this boilerplate code.
Brian,
共享 EntityManagerFactory 可以,但共享 EntityManager 不行。请参阅 Java 持久性 wiki 手册。
更现代的方法是使用枚举来实现单例。因此,假设您不希望四处更改持久性单元名称(我希望这种情况无论如何都不会发生)...
所以理想情况...
使用单例机制的枚举可以提供... (有效Java 第二版)
然后就像请求 EntityManager 一样简单...
您可以根据需要添加更多持久性单元。
Brian,
It is okay to share EntityManagerFactory, but it is not okay to share EntityManager. See the Java Persistence wikibook.
A more modern approach would be to do singletons with enums. So, assuming you don't expect to go around changing your persistence unit names (I would expect that not to occur anyhow)...
So ideally...
Using an enum for the singleton mechanism provides for... (Effective Java Second edition)
Then it's as simple as asking for an EntityManager...
You can add more persistence units as you need them.