Created
May 24, 2013 06:00
-
-
Save timothyklim/5641544 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package test.monads | |
| import scalaz._, Scalaz._, effect._, IO._ | |
| import javax.persistence.EntityManager | |
| import javax.persistence.EntityManagerFactory | |
| import javax.persistence.Persistence | |
| import scala.collection.JavaConversions._ | |
| object testMonadicHibernate { | |
| val entityManager = IO { | |
| val entityManagerFactory: EntityManagerFactory = Persistence.createEntityManagerFactory( "test.monads" ) | |
| entityManagerFactory.createEntityManager() | |
| } | |
| def startTransaction(t: IO[EntityManager]) = {t.map(x => {x.getTransaction.begin;x})} | |
| val startTransactionState: State[IO[EntityManager], Unit] = for { | |
| a <- get | |
| _ <- put(startTransaction(a)) | |
| } yield () | |
| def commitTransaction(t: IO[EntityManager]) = {t.map(x => {x.getTransaction.commit;x})} | |
| val commitTransactionState: State[IO[EntityManager], Unit] = for { | |
| a <- get | |
| _ <- put(commitTransaction(a)) | |
| } yield () | |
| def persist(t: IO[EntityManager], entity: java.lang.Object) = {t.map(x => {x.persist(entity);x})} | |
| def persistEntity(entity: java.lang.Object): State[IO[EntityManager], Unit] = for { | |
| a <- get | |
| _ <- put(persist(a, entity)) | |
| } yield () | |
| def query(t: IO[EntityManager], hsqlquery: String) = {t.map(x => {val y = x.createQuery(hsqlquery);y})} | |
| def queryEntity(hsqlquery: String): State[IO[EntityManager], IO[javax.persistence.Query]] = for { | |
| a <- get[IO[EntityManager]] | |
| val b = query(a, hsqlquery) | |
| } yield b | |
| val entityManagerState: State[IO[EntityManager], Unit] = for { | |
| e <- init | |
| } yield () | |
| def main(args: Array[String]): Unit = { | |
| val hibernateAction1 = for { | |
| a <- entityManagerState | |
| b <- startTransactionState | |
| val addres1 = new Address("Address1") | |
| c <- persistEntity(addres1) | |
| val addres2 = new Address("Address2") | |
| d <- persistEntity(addres2) | |
| val addres3 = new Address("Address3") | |
| e <- persistEntity(addres3) | |
| val addres4 = new Address("Address4") | |
| f <- persistEntity(addres4) | |
| g <- commitTransactionState | |
| } yield g | |
| val res1 = hibernateAction1(entityManager) | |
| res1._1.unsafePerformIO | |
| val hibernateAction2 = for { | |
| a <- hibernateAction1 | |
| b <- startTransactionState | |
| val addres5 = new Address("Address5") | |
| c <- persistEntity(addres5) | |
| val addres6 = new Address("Address6") | |
| d <- persistEntity(addres6) | |
| val addres7 = new Address("Address7") | |
| e <- persistEntity(addres7) | |
| val addres8 = new Address("Address8") | |
| f <- persistEntity(addres8) | |
| g <- commitTransactionState | |
| } yield g | |
| val res2 = hibernateAction2(entityManager) | |
| res2._1.unsafePerformIO | |
| val hibernateAction3 = for { | |
| a <- hibernateAction2 | |
| b <- queryEntity("From Address") | |
| } yield b | |
| val res3 = hibernateAction3(entityManager) | |
| val res = res3._2.unsafePerformIO | |
| res.getResultList().toList.map{x => println(x.asInstanceOf[Address].name)} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment