【问题标题】:Play Framework 2.4.x - Subprojects with databasePlay Framework 2.4.x - 带有数据库的子项目
【发布时间】:2015-08-16 17:33:09
【问题描述】:

大家好,

希望您能真正帮助我处理 Play Framework 2.4.x 中的子项目。我正在开发一个带有子项目的 Play 项目(我称之为 root)。两者都有 ebean 模型,我想将这些模型保存在不同的数据库中。我尝试了很多可能性,但我无法解决。

  1. 在 [root]/conf/application.conf 中定义一个数据库和 Ebean 配置,在 [root]/modules/sub/conf/application.conf 中定义另一个(使用不同的数据库名称)。然后报错“CreationException: Unable to create injector,看到如下错误:1) Error injection constructor, java.lang.IllegalStateException: Bean class models.RootModel is not enhanced?”
  2. 在 root 的配置中定义一个数据库和 Ebean 配置,在子项目的配置中定义一个具有相同数据库名称的数据库。然后我收到一个错误“PersistenceException: subproject.models.SubModel 不是在此服务器上注册的实体 Bean?”
  3. 在根项目中定义数据库和 Ebean 配置,并在其配置中为子项目定义数据库,与 1 中相同的错误。
  4. 我的子项目没有配置,错误:"CreationException: Unable to create injector,看到如下错误:1) Error injection constructor, java.lang.IllegalStateException: Bean class subproject.models.SubModel 没有增强? "

如何为 Play Framework 项目及其子项目设置数据库?

我的文件在这些文件夹中:

    [root]/build.sbt
    [root]/conf/application.conf
    [root]/app/models/RootModel.java
    [root]/modules/sub/conf/application.conf
    [root]/modules/sub/conf/app/models/subproject/models/SubModel.java

我的 [root]/build.sbt:

    import com.typesafe.play.sbt.enhancer.PlayEnhancer

    name := """rootproject"""

    version := "1.0"

    lazy val root = (project in file("."))
        .enablePlugins(PlayJava, PlayEbean, PlayEnhancer)
        .aggregate(sub)
        .dependsOn(sub) 
        .settings(
            TwirlKeys.templateImports += "subproject.models._"
         )

     lazy val sub = project.in(file("modules/sub"))
        .enablePlugins(PlayJava, PlayEbean, PlayEnhancer)

     scalaVersion := "2.11.6"

在application.conf中定义数据库和ebean配置:

    db.default.driver=org.h2.Driver
    db.default.url="jdbc:h2:./db/default;DB_CLOSE_DELAY=-1"
    db.default.username="sa"
    db.default.password="..."

    db.sub.driver=org.h2.Driver
    db.sub.url="jdbc:h2:./db/sub;DB_CLOSE_DELAY=-1"
    db.sub.username="sa"
    db.sub.password="..."

    ebean.default=["models.*"]
    ebean.sub=["subproject.models.*"]

【问题讨论】:

    标签: java playframework subproject


    【解决方案1】:

    好的,我自己想通了。很简单,看Play Framework Documentation

    如果您遇到“PersistenceException:sub.model.SubModel 不是在此服务器上注册的实体 Bean?”之类的问题,请查看 Multiple Databases with Play Framework 2.1.x

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多