Hibernate EntityManager,它应该被用作单例吗? [英] Hibernate EntityManager, is it supposed to be used as a singleton?

查看:345
本文介绍了Hibernate EntityManager,它应该被用作单例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



我不是100%确定Hibernate是如何与EntityManager协同工作的,所以我想知道是否可以这么多(EntityManager),例如,会有交易问题吗?



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



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

从我尝试使用的代码片段中获取@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 =manager1transaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>

<属性>

< property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
< property name =javax.persistence.jdbc.uservalue =root/>
< property name =javax.persistence.jdbc.passwordvalue =mypassword/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// localhost / ptbrowserdb/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< / properties>
< / persistence-unit>
< /余辉>


解决方案

看到这篇文章: JPA Architecture 它可以很好地解释它。



总的来说你需要每个事务都有一个实体管理器。此实体管理器不得同时在两个事务中使用。

Clairification:我的意思是,不要将单个实体管理器用于不同的单元作品。典型的一个工作单元中的一项交易,如果您有一个单位工作的不同交易,那么您可以使用相同的实体经理



如果您使用如果使用 @PersistenceContext 注释来注入EntityManager,那么Spring和Spring会为您执行此操作。默认情况下,Spring将注入的EntityManager(通过代理)绑定到当前事务。 (并且交易绑定到线程。)



@See Spring Reference 13.5.2基于普通JPA实现DAO - 它包含一个有趣的paragagraph,代码示例。

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>

解决方案

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.

这篇关于Hibernate EntityManager,它应该被用作单例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆