title: Execute JPA query on GlobalSetting.onStart() in PLayframework
body: <p>How can I do some db operation on playframework global class method on start.</p>
  
  <p>I would like to put some data to db from other source.
  For now I have:</p>
  
  <p>My code:</p>
  
  <pre><code>public class Global extends GlobalSettings {
  
  
      @Override
      @Transactional
      public void onStart(Application application) {
                      Akka.system().scheduler().schedule(
                              Duration.create(1, TimeUnit.SECONDS), // start task delay
                              Duration.create(24, TimeUnit.HOURS), // between task instance delay
                              //Duration.create(24, TimeUnit.HOURS), // between task instance delay
                              new Runnable() {
                                  @Override
                                  @Transactional
                                  public void run() {
                                      System.out.println("Importing CRM data...");
                                      ImportCrmData.start();
                                      System.out.println("... imported");
                                  }
                              },
                              Akka.system().dispatcher()
                      );
  </code></pre>
  
  <p>I am getting an error:</p>
  
  <pre><code>[info] play - Shutdown application default Akka system.
  [info] play - datasource [jdbc:mysql://localhost/svp] bound to JNDI as DefaultDS
  [info] play - datasource [jdbc:mysql://192.168.0.4/scrm_customer] bound to JNDI as CRM
  [info] play - database [default] connected at jdbc:mysql://localhost/svp
  [info] play - database [crm] connected at jdbc:mysql://192.168.0.4/scrm_customer
  [info] play - Starting application default Akka system.
  [info] play - Application started (Dev)
  Importing CRM data...
  [ERROR] [12/08/2014 15:57:04.773] [application-akka.actor.default-dispatcher-3] [TaskInvocation] No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.
  java.lang.RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.
          at play.db.jpa.JPA.em(JPA.java:55)
          at models.Customer.getByCrmId(Customer.java:185)
          at models.CustomerCRM.importCrmData(CustomerCRM.java:84)
          at actions.ImportCrmData.start(ImportCrmData.java:28)
          at Global$1.run(Global.java:40)
          at akka.actor.LightArrayRevolverScheduler$$anon$3$$anon$2.run(Scheduler.scala:241)
          at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
          at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
          at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
          at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
          at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
          at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
  </code></pre>
  
  <p>Please give me some help.</p>
  
  <p>I use playframework 2.2.4 with java</p>
  
  <p>-------------------------------------EDIT-----------------------------------------</p>
  
  <p>Afted adding:</p>
  
  <pre><code>JPA.withTransaction(() -&gt; {
      ImportCrmData.start();
  });
  </code></pre>
  
  <p>I am getting error.</p>
  
  <pre><code>[info] Compiling 1 Java source to app/modules/common/target/scala-2.10/classes...
  [error] app/modules/common/app/Global.java:38: error: illegal start of expression
  [error]                                                 JPA.withTransaction(() -&gt; {
  [error]                                                                      ^
  [error] app/modules/common/app/Global.java:38: error: illegal start of expression
  [error]                                                 JPA.withTransaction(() -&gt; {
  [error]                                                                         ^
  [error] app/modules/common/app/Global.java:38: error: illegal start of expression
  [error]                                                 JPA.withTransaction(() -&gt; {
  [error]                                                                           ^
  [error] app/modules/common/app/Global.java:38: error: ';' expected
  [error]                                                 JPA.withTransaction(() -&gt; {
  [error]                                                                            ^
  [error] app/modules/common/app/Global.java:39: error: illegal start of expression
  [error]                                                     ImportCrmData.start();
  [error]                                                                  ^
  [error] app/modules/common/app/Global.java:39: error: ';' expected
  [error]                                                     ImportCrmData.start();
  [error]                                                                        ^
  [error] app/modules/common/app/Global.java:40: error: illegal start of type
  [error]                                                 });
  [error]                                                  ^
  [error] app/modules/common/app/Global.java:41: error: ')' expected
  [error]                                             }
  [error]                                              ^
  [error] app/modules/common/app/Global.java:42: error: illegal start of expression
  [error]                                         },
  [error]                                          ^
  [error] app/modules/common/app/Global.java:43: error: ';' expected
  [error]                                         Akka.system().dispatcher()
  [error]                                             ^
  [error] app/modules/common/app/Global.java:43: error: ';' expected
  [error]                                         Akka.system().dispatcher()
  [error]                                                                   ^
  [error] 11 errors
  [error] (common/compile:compile) javac returned nonzero exit code
  </code></pre>
  
  <p>Finally I've added:</p>
  
  <pre><code>                        public void run() {
                              JPA.withTransaction(
                                      ImportCrmData.start()
                                      );
                          }
  </code></pre>