Skip to content

Instantly share code, notes, and snippets.

@cwfoo
Forked from daniel-shuy/EntityManagerProducer.java
Created February 25, 2020 06:42
Show Gist options
  • Save cwfoo/e30464cff6b2aa350fe98664fe00e61e to your computer and use it in GitHub Desktop.
Save cwfoo/e30464cff6b2aa350fe98664fe00e61e to your computer and use it in GitHub Desktop.
JPA + CDI : @Inject EntityManager as a @RequestScoped bean
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Dependent
public class EntityManagerProducer {
@PersistenceContext(name = "persistence-unit") // name is Persistence Unit Name configured in persistence.xml
private EntityManager entityManager;
@Produces
@RequestScoped
EntityManager createEntityManager() {
return entityManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment