Created
November 7, 2014 17:41
-
-
Save rafaeltuelho/a0b53551c22246ad7b25 to your computer and use it in GitHub Desktop.
Creating an JPA EntityManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in JavaSE environment | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sample"); | |
EntityManager em = emf.createEntityManager(); | |
try{ | |
... | |
] | |
finally{ | |
em.close(); | |
emf.close(); | |
} | |
// in JavaEE environment | |
// the name is optional when your persistence.xml descriptor has only one PU | |
@PersistenceContext(unitName="sample") | |
EntityManager em; | |
// when you wish, for some particular reason, create your own EntityManager in a JavaEE env, inject the EMF | |
@PersistenceUnit(unitName="sample") | |
EntityManagerFactory emf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment