Reputation: 12876
We're currently using a problematic setup of our EntityManager
like in the following. We'd like to change from @PersistenceUnit
to @PersistenceContext
for the entity mangager. How can we use the same @Inject
mechanism?
persistence.xml:
<persistence-unit name="my-data-source" transaction-type="JTA">
<jta-data-source>java:/foo/model</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="${hibernate.show_sql:false}"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.order_inserts" value="true" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.jdbc.batch_size" value="30" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57InnoDBDialect" />
</properties>
</persistence-unit>
That's how we get the entity manager:
@Stateless
public class EntityManagerProvider {
@PersistenceUnit(unitName = "my-data-source")
private EntityManagerFactory emFactory;
@Produces
@Default
public EntityManager getDefaultEntityManager() {
return emFactory.createEntityManager();
}
}
And within the application we inject the entity manager like this:
@Inject
private EntityManager entityManager;
Upvotes: 0
Views: 866
Reputation: 7045
Your code is like bellow:
@PersistenceContext
private EntityManager entityManager;
Here import's are given bellow :
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
For more info, check this answer
Upvotes: 1
Reputation: 2952
I think you can just call it like this, of course without calling EntityManagerFactory:
@PersistenceContext
EntityManager em;
In this case the container will handle the life cycle of the entity manager bean, so you don't need to call EntityManagerFactroy.
Upvotes: 1