JPA EntityManager,它是如何工作的?
抱歉,我的问题很菜鸟,但我在使用 JPA+Hibernate 时遇到了问题,所以我认为我的想法有些不清楚。 我有一些实体,比如 A、B、C、D,并且我编码了 AMethods、BMethods、CMethods、DMethods。每个 *Methods 类都包含通过 EntityManagerFactory 进行的 EntityManager 初始化以及一些基本上执行查询的方法。我不知道是否应该使用单例模式(以便每个 *Method 类都有一个 EntityManager),或者是否需要在每次执行查询或保留/删除实体时打开和关闭 EntityManager...你能帮助我吗??
Sorry for the noob question, but I'm having problems with JPA+Hibernate so I thought that something is not clear in my mind.
I have some entities, say A, B, C, D and I have coded AMethods, BMethods, CMethods, DMethods. Each of the *Methods classes contain EntityManager initialization via EntityManagerFactory and some methods that basically execute queries. I don't know if I should use a singleton pattern (so that I have an EntityManager per *Method class) or if I need to open and close the EntityManager each time I execute a query or I persist/remove an entity... can you help me??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在典型的 JPA/Hibernate 应用程序中,您不会将持久性逻辑放入实体类本身中。与较旧的 EJB 2.x 应用程序相比,这是设计理念的重大变化。相反,许多应用程序创建一个与实体分离的数据访问对象层,该层使用 EntityManager 实例来查询、加载和保存实体。通常,这些都是单例,并且 DAO 内的实体管理器实例对于线程来说是本地的。
如果您使用像 Spring 这样的框架,EntityManager 实例和事务的管理是完全自动的。与 EJB 3 相同,尽管我没有在大型项目中使用过它。我建议阅读 Spring 文档的关于 对象关系映射数据访问。即使您最终没有在应用程序中使用 Spring,本章也提供了一些关于如何以分层方式构建应用程序的好技巧,从而将持久性问题与要持久化的实体分开。祝你好运!
In a typical JPA/Hibernate application, you don't put persistence logic in the entity classes themselves. This is a big change in design philosophy compared to older EJB 2.x applications. Instead, many applications create a layer of Data Access Objects--separate from the entities--that use EntityManager instances to query, load, and save entities. Often, these are singletons, and the entity manager instances inside the DAOs are local to the thread.
If you use a framework like Spring, the management of the EntityManager instances and transactions is completely automatic. Same with EJB 3, although I have not used that on a large project. I would suggest reading the Spring documentation's chapter on Object-Relational Mapping data access. Even if you don't end up using Spring in your application, the chapter gives some good tips on how to structure your application in a layered way that separates persistence concerns from the entities being persisted. Good luck!
EntityManager 与持久性上下文关联。如果您的所有实体都与一个上下文关联,请使用单例模式。
你在服务器端使用jpa,对吗?如果是这样,您可以在 init 方法中初始化 EntityManager,例如 servlet 上的 init()。
EntityManager is associated with a persistence context. Use a singleton pattern if all of yours entities are associated with one context.
You use jpa on server side,right? If so you can initialized EntityManager in init methods, like init() on servlets.
就像这样!
公共接口 ProtokollDAOService {
}
公共类 ProtokollDAOImpl 实现 ProtokollDAOService {
私有静态最终字符串 PERSISTENCE_UNIT_NAME = "ProtokollManager";
私有 EntityManagerFactory 实体工厂;
}
just like this!
public interface ProtokollDAOService {
}
public class ProtokollDAOImpl implements ProtokollDAOService {
private static final String PERSISTENCE_UNIT_NAME = "ProtokollManager";
private EntityManagerFactory entityFactory;
}