2012-01-06 60 views
3

在我的项目中,我使用了Seam 3,并且我注意到注入了EntityManager@Inject注释。我很确定有一些配置可以确保EnityManager知道要使用哪个PersistenceUnit。例如用EJB可以键入:配置能够在@Seam中注入EntityManager 3

@PersistenceContext(unitName="MY_PERSISTENCE_UNIT_NAME") 
private EntityManager eManager; 

其持久性单元在persistence.xml文件配置。这里是我的伪配置:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
    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"> 

    <persistence-unit name="MY_PERSISTENCE_UNIT_NAME" transaction-type="JTA"> 

     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <jta-data-source>java:jboss/TimeReportDS</jta-data-source> 
     <mapping-file>META-INF/orm.xml</mapping-file> 

     <class>....</class> 
     <class>....</class> 
     <class>....</class> 

     <properties> 

      <property name="jboss.entity.manager.factory.jndi.name" 
       value="java:/modelEntityManagerFactory" /> 

      <!-- PostgreSQL Configuration File --> 
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" /> 
      <property name="hibernate.connection.password" value="password" /> 
      <property name="hibernate.connection.url" value="jdbc:postgresql://192.168.2.125:5432/t_report" /> 
      <property name="hibernate.connection.username" value="username" /> 

      <!-- Specifying DB Driver, providing hibernate cfg lookup 
       and providing transaction manager configuration --> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> 
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> 
      <property name="hibernate.transaction.manager_lookup_class" 
       value="org.hibernate.transaction.JBossTransactionManagerLookup" /> 
      <property name="hibernate.archive.autodetection" value="class" /> 

      <!-- Useful configuration during development - developer can see structured SQL queries --> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="false" /> 

     </properties> 
    </persistence-unit> 
</persistence> 

我已经加入了阅读接缝2的一些文章,但没有配置在components.xml文件由:

<persistence:managed-persistence-context 
     name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/modelEntityManagerFactory" /> 

<components>标签内。在接缝2的下一个步骤是添加:

<property name="jboss.entity.manager.factory.jndi.name" 
       value="java:/modelEntityManagerFactory" /> 
persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence ...> 
    <persistence-unit name="MY_PERSISTENCE_UNIT_NAME" ...> 
     ... 
     <properties> 
      ... 

      <property name="jboss.entity.manager.factory.jndi.name" 
       value="java:/modelEntityManagerFactory" /> 

     </properties> 
    </persistence-unit> 
</persistence> 

但接缝,在煤层3没有文件components.xml@Inject注释中也没有unitName的属性来指定持久性单元。

因此,请帮助我配置我的项目,以便我可以使用@InjectEntityManager,如网上的许多示例所示。

我使用Postgres数据库和JBoss AS 7

编辑:添加一个例子。我不在Entity类中使用EntityManager

@Named("validateReportAction") 
@SessionScoped 
public class ValidateReportAction extends ReportAction implements Serializable { 

    private static final long serialVersionUID = -2456544897212149335L; 

    @Inject 
    private EntityManager em; 
... 
} 

在这里,在这个@Inject我从EclipseNo bean is eligible for injection to the injection point [JSR-299 §5.2.1]

警告,如果我上了些豆子,被标记为Entity@Inject工作正常使用@Inject

+0

你有什么问题?如果在同一个persistence.xml中有多个持久性单元,则只需在注释中使用鉴别符。因为它只是一个默认值。确保你的persistence.xml和你的DAO和Entity类在同一个模块(JAR)中。 – Perception 2012-01-06 19:24:43

+0

@Perception请看看我编辑的帖子,并给我一些鉴别器的例子。 – nyxz 2012-01-06 20:35:14

+0

Maistora - 你不需要鉴别注释,你只有一个PU在你的persistence.xml。但基于您添加的错误消息,我很确定Eclipse没有找到您的JBoss JEE实现文件。如果您使用的是Maven,那么您需要将jboss-javaee-web-6.0依赖项添加到您的POM中。如果您使用其他构建工具,那么您需要手动查找并添加JAR到您的项目类路径。 – Perception 2012-01-06 20:49:22

回答

5

您可以在CDI bean上使用@PersistenceContext。它不一定是一个EJB。

如果由于某种原因想要使用@Inject,则必须做更多的工作。 @Inject不知道EntityManager;它只能注入其他托管的bean。令人高兴的是,有一个简单的解决方法 - 使用一个充当简单蹦床的生产者方法。

@ApplicationScoped 
public class EntityManagerProducer { 

    @PersistenceContext 
    private EntityManager entityManager; 

    @Produces 
    @RequestScoped 
    public EntityManager getEntityManager { 
     return entityManager; 
    } 

    public void closeEntityManager(@Disposes EntityManager em) { 
     if (em != null && em.getTransaction().isActive()) { 
      em.getTransaction().rollback(); 
     } 
     if (em != null && em.isOpen()) { 
      em.close(); 
     } 
    } 

} 

您现在可以使用@Inject来注入EntityManager。注入的EntityManager将为RequestScoped,而EntityManagerProducer为ApplicationScoped。此外,entityManager必须关闭。

+0

这是很不错的:)谢谢,但是当我想用entityManager.persist(myObject的),并且此错误出现:javax.persistence.TransactionRequiredException:事务需要执行此操作(无论是使用事务或扩展的持久化上下文) – nyxz 2012-01-07 13:43:30

+0

所以我添加了PersistenceContext(type = PersistenceContextType.EXTENDED),但没有发生任何事情。我读到这意味着我应该自己关心交易。有没有办法让容器管理交易? – nyxz 2012-01-07 13:45:57

+1

不怕。 EJB获取事务; CDI豆没有。 Seam可能对此有所帮助,但我对Seam了解不多。在简单的EE中,您必须自己管理事务。您的选项有(一)在bean通过UserTransaction的管理事务明确,(B)添加过滤器和整个包住请求周期在一个事务中,以及(c)编写管理事务的CDI拦截器(请参见http:// smokeandice .blogspot.com/2009/12/cdi-and-declarative-transactions.html - 这真的很酷!)。 – 2012-01-07 22:20:45