播放框架Ebean错误Java.lang.RuntimeException:DataSource用户是否为空?

时间:2017-07-24 01:17:02

标签: java playframework ebean

我想自动创建数据表,所以我在类构造函数中编写.save()。 型号:

@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)

希望有任何建议!谢谢!

2 个答案:

答案 0 :(得分:0)

您的ServerConfig Ebean&#34; mydata&#34;。看起来似乎ebean无法找到您的配置数据源&#34; mydata&#34;。

  

要自定义基础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

答案 1 :(得分:0)

当前版本(Gradle 6 +,Play 2.6,Ebean 12 +,Java 8)中的另一个来源是需要io.ebean插件和play-ebean库依赖项的组合。

plugins {
    id 'idea'
    id 'distribution'
    id 'org.gradle.playframework' version '0.9'
    id('io.ebean') version '12.1.8'
}
dependencies {
    // Consider downgrading ebean
    // https://github.com/playframework/play-ebean/issues/194
    // Ebean failures = https://github.com/ebean-orm-tools/ebean-gradle-plugin
    implementation group: 'com.typesafe.play', name: 'play-ebean_2.12', version: '4.1.4'

    implementation 'com.google.guava:guava:22.0'
    implementation 'org.bouncycastle:bcprov-jdk15on:1.60'
    implementation 'org.avaje.composite:logback:1.1'
    implementation 'org.xerial:sqlite-jdbc:3.18.0'
    implementation 'mysql:mysql-connector-java:5.1.41'
    // PROVEN fix for: java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
    implementation 'javax.xml.bind:jaxb-api:2.2.4'

    ['play-java', 'play-java-forms', 'play-java-jpa', 'play-jdbc', 'play-jdbc-evolutions',
     'filters-helpers', 'play-logback',  'play-guice'].each { playModule ->
            implementation "com.typesafe.play:${playModule}_$scalaV:$playV"
    }

    implementation "io.ebean:ebean:${ebeanV}"
    implementation "io.ebean:ebean-querybean:${ebeanV}"
    annotationProcessor "io.ebean:querybean-generator:${ebeanV}"
    testImplementation "io.ebean:ebean-test:${ebeanV}"
    testImplementation "io.ebean.test:ebean-test-config:11.41.2" // TODO Test fix test

    // apt query bean generation
    testImplementation 'org.avaje.composite:junit:1.1'
    testImplementation 'org.bouncycastle:bcprov-jdk15on:1.60'
    testImplementation 'mysql:mysql-connector-java:5.1.41'
    testImplementation 'org.clojure:java.jdbc:0.7.11'
    testImplementation 'org.xerial:sqlite-jdbc:3.18.0'

}

欢呼