Home > Enterprise >  scala compiling error: Cannot resolve overloaded method 'withTransaction'
scala compiling error: Cannot resolve overloaded method 'withTransaction'

Time:09-17

I'm new to Scala, and I can't figure out how to solve a compiling error for method withTransaction :

Cannot resolve overloaded method 'withTransaction'

object Global {
  
   def goBootstrap(app: Application) {
    Logger.info(" ****   start *****")
     onGet();

    }

  def onGet() {
    import play.db.jpa.JPA
    Logger.info("Cnnection start");
    JPA.withTransaction(JPA.em =>
    {
      val resultsList = JPA.em.createNamedQuery("findCity").setParameter("name", "Boston").getResultList
    }
    );
    
  }

}
      

This code snippet is located in a Global.scala file in Play project (version 2.3.X). JPA came from import play.db.jpa.JPA

How can I solve this compiling error?

CodePudding user response:

The error is telling you that there is no method on JPA whose signature matches the parameters you're passing. You are calling JPA.withTransaction( () => Unit).

Looking at the source there are three methods withTransaction with Unit return types:

void withTransaction(Consumer<EntityManager> block);
void withTransaction(String name, Consumer<EntityManager> block);
void withTransaction(String name, boolean readOnly, Consumer<EntityManager> block);

I'm going to assume that you're trying to use the first of those methods. Looking at the docs for Consumer it requires a single argument.

In short, you need to provide an input to your block, something like:

   JPA.withTransaction(JPA.em => {
      val resultsList = JPA.em.createNamedQuery("findCity").setParameter("name", name).getResultList
    });

CodePudding user response:

The problem is that you can't directly instantiate a JPA connection through scala. Also because the play 2.3 framework does not support this feature: https://www.playframework.com/documentation/2.3.x/ScalaHome

  • Related