Skip to content

Instantly share code, notes, and snippets.

@rafaeltuelho
Created November 7, 2014 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeltuelho/a0b53551c22246ad7b25 to your computer and use it in GitHub Desktop.
Save rafaeltuelho/a0b53551c22246ad7b25 to your computer and use it in GitHub Desktop.
Creating an JPA EntityManager
// 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