2017-07-24 82 views
0

我想自動創建數據表,因此我在類構造函數中編寫了.save()。 型號:Play框架Ebean錯誤Java.lang.RuntimeException:DataSource用戶爲空?

@Entity 
@Table(name="info") 
public class Info extends Model{ 

    public String name; 
    public String page; 

    public Info(String name, String page) { 
     super(); 
     this.name = name; 
     this.page = page; 
    } 
    public Info(){ 
     new Info("Book1","100").save(); 
    } 
    @Override 
    public void save() { 
     try{ 
      Ebean.getServer("mydata").save(this); 
     }catch (Exception e) { 
      throw e; 
     } 
    } 
} 

application.conf:

ebean.default="models.*" 
ebean.mydata="mymodels.*" 

但我得到以下

javax.persistence.PersistenceException: java.lang.RuntimeException: DataSource user is null? 
    com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.setEntityBeanClass(BeanDescriptorManager.java:1458) 
    com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.createByteCode(BeanDescriptorManager.java:1150) 
    com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readDeployAssociations(BeanDescriptorManager.java:1070) 
    com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readEntityDeploymentAssociations(BeanDescriptorManager.java:547) 
    com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.deploy(BeanDescriptorManager.java:237) 
    com.avaje.ebeaninternal.server.core.InternalConfiguration.<init>(InternalConfiguration.java:114) 
    com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:204) 
    com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:65) 
    com.avaje.ebean.EbeanServerFactory.create(EbeanServerFactory.java:58) 
    play.db.ebean.EbeanPlugin.onStart(EbeanPlugin.java:79) 
    play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) 
    play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) 
    scala.collection.immutable.List.foreach(List.scala:318) 
    play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:88) 
    play.api.Play$$anonfun$start$1.apply(Play.scala:88) 
    play.api.Play$$anonfun$start$1.apply(Play.scala:88) 
    play.utils.Threads$.withContextClassLoader(Threads.scala:18) 
    play.api.Play$.start(Play.scala:87) 
    play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:139) 
    play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) 
    scala.Option.map(Option.scala:145) 
    play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) 
    play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) 
    scala.util.Success.flatMap(Try.scala:200) 
    play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:110) 
    play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:102) 
    scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) 
    scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) 
    scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361) 
    scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) 
    scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) 
    scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) 
    scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) 

希望的任何建議的錯誤!謝謝!

回答

0

你的ServerConfig Ebean「mydata」在哪裏。看起來似乎ebean找不到你的配置數據源「mydata」。

要自定義底層Ebean服務器配置,您可以添加的conf/ebean.properties文件,或創建ServerConfigStartup接口的實例編程操作Ebean SERVERCONFIG服務器初始化之前。
For more information. Check play document

但如果你只有一臺數據庫服務器,您可以配置使用默認的數據源。例如:

db { 
    default { 
    hikaricp { 
     dataSourceClassName = org.mariadb.jdbc.MariaDbDataSource 
     dataSource { 
     user = "yourUsername" 
     password = "masked" 
     databaseName = "yourDB" 
     serverName = "yourServer" 
     } 
    } 
    } 
} 

ebean.default = ["models.*"] 

Full config at here