Hibernate EntityManager,它应该用作单例吗?

发布于 2025-01-07 07:21:42 字数 1601 浏览 1 评论 0原文

我没有使用 Spring,因此我在类中创建 EntityManager 的实例。

我使用 Hibernate-Eclipse 逆向工程来自动生成类。这些类都有一个 EntityManager 实例。

我不是 100% 确定 Hibernate 如何与 EntityManager 一起工作,所以我想知道创建这个类(EntityManager)的这么多实例是否可以,例如,事务会出现问题吗?

我是否应该创建一个单独的类来为所有其他类分配 EntityManager 的静态实例?或者没关系?

编辑:我看到有一个叫做@PersistenceContext的东西,它似乎没有将我的persistence.xml作为bean加载到实例变量中,这个功能是否需要spring? (我得到空指针异常,因为它从未被注入)

我尝试使用 @persistencecontext

@PersistenceContext(unitName = "manager1")
private EntityManager entityManager;

我的 persistence.xml的代码片段

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <properties>

         <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
         <property name="javax.persistence.jdbc.user" value="root"/>
         <property name="javax.persistence.jdbc.password" value="mypassword"/>
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/ptbrowserdb"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      </properties>
   </persistence-unit>
</persistence>

I am not using Spring so I am creating an instance of EntityManager within a class.

I used Hibernate-Eclipse reverse engineering to auto-generate the classes. These classes all has an instance of EntityManager.

I am not 100% sure how Hibernate works with the EntityManager so I am wondering if it is okay that so many instances of this class (EntityManager) are made, for example, will there be problems with transactions?

Should I just make a separate class that distributes a static instance of an EntityManager for all my other classes? or does it not matter?

EDIT: I see there's something called @PersistenceContext, it doesn't seem to load my persistence.xml as a bean in to the instance variable, does this feature require spring? (I get null pointer exception, because it was never injected)

snip of code from where I attempt to use @persistencecontext

@PersistenceContext(unitName = "manager1")
private EntityManager entityManager;

my persistence.xml

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <properties>

         <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
         <property name="javax.persistence.jdbc.user" value="root"/>
         <property name="javax.persistence.jdbc.password" value="mypassword"/>
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/ptbrowserdb"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      </properties>
   </persistence-unit>
</persistence>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2025-01-14 07:21:42

请参阅这篇文章:JPA 架构 它解释得很好。

一般来说,每个事务都需要一个实体管理器。并且这个Entity Manager不能同时在两个事务中使用。

澄清:我的意思是,不要对不同的工作单元使用单个实体管理器。典型的一个工作单元中的一个事务,如果一个工作单元有不同的事务,那么您可以使用相同的实体管理器

如果您使用 Spring,那么如果您使用 @PersistenceContext 注解来注入 EntityManager。默认情况下,Spring 将注入的 EntityManager(通过代理)“绑定”到当前事务。 (并且事务“绑定”到线程。)

@See Spring 参考 13.5.2 基于普通 JPA 实现 DAO - 它在代码示例后面包含一个有趣的段落。

See this Article: JPA Architecture it explain it very well.

In General you need a single Entity Manager per transaction. And this Entity Manager must not be used in two transactions at the same time.

Clairification: I mean, do not use a single Entity Manager for different unit of works. Typical one transaction in one unit of work, if you have different transactions of one unit of work, then you can use the same Entity Manager

If you use Spring then Spring do this handling for you if you use the @PersistenceContext annotation to inject the EntityManager. Per default Spring "bind" the the injected EntityManager (via a proxy) to the current transaction. (And the transaction is "bound" to the thread.)

@See Spring Reference 13.5.2 Implementing DAOs based on plain JPA - it contains a interesting paragagraph after the code examples.

七婞 2025-01-14 07:21:42

您需要像 Spring 或 Google Guice 这样的依赖注入框架来将对象注入到您的类中,否则它可能不会自动为您注入。

基本上,这是 JPA 提供的注释,它将与 hibernate 或任何其他 ORM 框架配合使用,但您需要 DI 框架来注入对象。

关于实体管理器的单个实例,如果您使用 Spring,我认为您不需要它,因为它通过将实体管理器与 jpa 事务绑定来负责管理实例和事务。

You need a dependency injection framework like Spring or Google Guice to inject objects into your class otherwise it may not be injected automatically for you.

Basically this is an annotation provided by JPA which will work with in tandem with hibernate or any other ORM framework per say but you need a DI framework to inject the objects.

Regarding the single instance of entity manager i don't think you need that if you go by Spring since it takes care of managing the instances and the transactions for you by tying your entity manager with the jpa transaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文