Skip to content

Instantly share code, notes, and snippets.

@odwrotnie
Created February 2, 2012 00:44
Show Gist options
  • Save odwrotnie/1720468 to your computer and use it in GitHub Desktop.
Save odwrotnie/1720468 to your computer and use it in GitHub Desktop.
JPA
import javax.persistence._
object JPA {
val unit = "jpa"
val emf = Persistence.createEntityManagerFactory(unit)
def t[R](f: EntityManager => R) = {
val em = emf.createEntityManager
val transaction = em.getTransaction
transaction.begin
// Before
val res = f(em)
// After
transaction.commit
em.close
res
}
def em[R](f: EntityManager => R) = {
val em = emf.createEntityManager
// Before
val res = f(em)
// After
em.close
res
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment