配置能够@在Seam 3中注入EntityManager [英] Configuration to be able to @Inject EntityManager in Seam 3
问题描述
在我的项目中,我使用 Seam 3
,并且我注意到 EntityManager
c> @Inject 注释。我确定有一些配置,以确保 EnityManager
知道使用 PersistenceUnit
。例如, EJB
可以输入:
In my project I use Seam 3
and I am having issues injecting the EntityManager
with the @Inject
annotation. I am pretty sure there is some kind of configuration to make sure the EnityManager
knows which PersistenceUnit
to use. For example with EJB
you can type:
@PersistenceContext(unitName="MY_PERSISTENCE_UNIT_NAME")
private EntityManager eManager;
哪些持久化单元配置在 persistence.xml
文件。这是我的伪配置:
which persistence unit is configured in the persistence.xml
file. Here is my pseudo configuration:
<?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>
我已经读了一些关于Seam 2的文章,但是配置在 components.xml
文件中添加:
I have read some articles about Seam 2 but there the configuration is made in components.xml
file by adding the:
<persistence:managed-persistence-context
name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/modelEntityManagerFactory" />
< Seam 2中的下一步是添加:
inside the <components>
tag. The next step in Seam 2 is to add the:
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/modelEntityManagerFactory" />
>
in the 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>
但是它在Seam 3中没有文件 components.xml
。此外,在 @Inject
注释中没有指定持久性单元的属性 unitName
。
but it seam that in Seam 3 there is no file components.xml
. Also there is no attribute unitName
in @Inject
annotation to specify the persistence unit.
因此,请帮助我配置我的项目,以便可以使用 @Inject
和 EntityManager
,如网上的很多示例所示。
So please help me to configure my project so I can use the @Inject
with EntityManager
as shown in many examples on the net.
我使用 Postgres
数据库和 JBoss AS 7
。
编辑:添加示例。我不在 Entity
类中使用 EntityManager
。
Adding an example. I don't use the EntityManager
in an Entity
class.
@Named("validateReportAction")
@SessionScoped
public class ValidateReportAction extends ReportAction implements Serializable {
private static final long serialVersionUID = -2456544897212149335L;
@Inject
private EntityManager em;
...
}
@Inject 我从 Eclipse
获得警告没有bean可以注入到注入点[JSR-299§5.2 .1]
如果我在一些bean上使用 @Inject
标记为实体
@Inject
可以正常工作。
If I use the @Inject
on some beans that are marked as Entity
the @Inject
works fine.
推荐答案
您可以在CDI bean上使用@PersistenceContext。它不一定是一个EJB。
You can use @PersistenceContext on a CDI bean. It doesn't have to be an EJB.
如果由于某种原因你想使用@Inject,你必须做更多的工作。 @Inject不知道EntityManager;它只能注入其他托管bean。幸运的是,有一个简单的解决方法 - 使用一个作为一个简单的蹦床的生产者方法。
If for some reason you want to use @Inject, you have to do more work. @Inject doesn't know about the EntityManager; it can only inject other managed beans. Happily, there is a simple workaround - use a producer method that acts as a simple trampoline.
@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必须关闭。
You can now use @Inject to inject the EntityManager. The injected EntityManager will be RequestScoped, while the EntityManagerProducer is ApplicationScoped. Furthermore, the entityManager must be closed.
这篇关于配置能够@在Seam 3中注入EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!