当多个数据库与播放框架

时间:2017-06-15 06:36:17

标签: database playframework ebean

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

我有两个以上的数据库,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();

1 个答案:

答案 0 :(得分:1)

I'm not sure if I understand your problem correctly. But i was facing a similar problem a few moments ago. I couldn't use the .save() method on a model that wasn't in the default ebean datasource. it just dropped the error:

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$1$$anonfun$applyOrElse$8$$anonfun$apply$8.apply(Routes.scala:307) ~[classes/:na]
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$8$$anonfun$apply$8.apply(Routes.scala:307) ~[classes/:na]
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136) ~[play_2.11-2.4.6.jar:2.4.6]
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.6.jar:2.4.6]
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70) ~[play_2.11-2.4.6.jar:2.4.6]
at play.http.DefaultHttpRequestHandler$1.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$7.apply(JavaAction.scala:94) [play_2.11-2.4.6.jar:2.4.6]
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) [play_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(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$2.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$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.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$1$$anonfun$apply$4.apply(Action.scala:104) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.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$1.apply(Action.scala:103) [play_2.11-2.4.6.jar:2.4.6]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96) [play_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537) [play-iteratees_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(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]

However, my workaround was that i've overridden the .save() method in the model:

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

I don't think that this is the most optimal solution for my problem, but it got my code working.