Implementing JPA query in Play for Scala

194 Views Asked by At

I'm trying to implement JPA in Play for Scala, following these examples documented in Java.

In the following code I get a compilation error in jpaApi.withTransaction:

class ManageBanks @Inject() (jpaApi: JPAApi) extends Controller {

   @Transactional
   def readMany = {
      val em = jpaApi.em

      jpaApi.withTransaction( em -> {   //  <-- error in this line
          val query = em.createQuery("from BankHib order by name")
          val list = query.getResultList.asScala.toList.map(_.asInstanceOf[BankHib])
          list
      })
   }
}

The error:

overloaded method value withTransaction with alternatives: (x$1: Runnable)Unit [T](x$1: java.util.function.Supplier[T])T [T](x$1: java.util.function.Function[javax.persistence.EntityManager,T])T cannot be applied to ((javax.persistence.EntityManager, List[admin.manage.BankHib]))

What's wrong with this code?

1

There are 1 best solutions below

0
On BEST ANSWER

Lambdas in Scala use syntax

em => ...

instead of

em -> ...

in Java.

-> in Scala is for maps.