当具有多个具有播放框架的数据库时不能使用 save()

can't use save() when has multiple database with play framework

当我尝试将模型实例保存到数据库时无法使用 instancename.save();

我有 2 个以上的数据库,A、B、C..似乎我没有将数据库设置为

ebean.default="models.*"

我不能使用 .save(); 但是 .save(); 当我 运行 子系统(只使用一个数据库)时可以工作。

当我使用 sql 查找时,我使用:

String sqlString = "select ....";       
list = Ebean.getServer("myDatabase").createSqlQuery(sql).findList();

我想我不能使用默认 .save() 因为我应该找到 Ebean 服务器但是我不知道如何处理它。

感谢您的帮助。

我的save()代码:

Customer a = new Customer;
a.name = "Mary";
a,age = 20;
a.save();

我不确定我是否正确理解了你的问题。但是我刚才遇到了类似的问题。我无法在不在默认 ebean 数据源中的模型上使用 .save() 方法。它只是丢弃了错误:

javax.persistence.PersistenceException: The type [class models.sprachtext.Sprachtext] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().
at com.avaje.ebeaninternal.server.persist.DefaultPersister.createRequest(DefaultPersister.java:1189) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebeaninternal.server.persist.DefaultPersister.update(DefaultPersister.java:168) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebeaninternal.server.persist.DefaultPersister.save(DefaultPersister.java:197) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1461) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1454) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebean.Model.save(Model.java:208) ~[avaje-ebeanorm-4.6.2.jar:na]
at models.sprachtext.Sprachtext.save(Sprachtext.java:67) ~[classes/:na]
at controllers.SprachtextController.save(SprachtextController.java:74) ~[classes/:2.4.6]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:307) ~[classes/:na]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:307) ~[classes/:na]
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:136) ~[play_2.11-2.4.6.jar:2.4.6]
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$$anon$$anon.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.6.jar:2.4.6]
at play.core.j.JavaAction$$anon.call(JavaAction.scala:70) ~[play_2.11-2.4.6.jar:2.4.6]
at play.http.DefaultHttpRequestHandler.call(DefaultHttpRequestHandler.java:20) ~[play_2.11-2.4.6.jar:2.4.6]
at actions.ExceptionsAction.call(ExceptionsAction.java:17) ~[classes/:na]
at actions.BasicAuthAction.call(BasicAuthAction.java:58) [classes/:na]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.6.jar:2.4.6]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:40) [play_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) [play_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.Future$.apply(Future.scala:31) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$.apply(Future.scala:492) [scala-library-2.11.6.jar:na]
at play.core.j.JavaAction.apply(JavaAction.scala:94) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.6.jar:2.4.6]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.6.jar:2.4.6]
at scala.Option.map(Option.scala:146) [scala-library-2.11.6.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) [play_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:537) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:537) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]

但是,我的解决方法是覆盖了模型中的 .save() 方法:

@Override
public void save() {
    Ebean.getServer("your-second-db-name").save(this);
}

我认为这不是我的问题的最佳解决方案,但它让我的代码正常工作。