【问题标题】:How do I set up 2 MySQL databases in Play Framework?如何在 Play Framework 中设置 2 个 MySQL 数据库?
【发布时间】:2013-07-29 12:48:49
【问题描述】:

我需要设置 2 个独立的数据库。一个用于Test类,另一个用于TestTwo类,但我不知道如何配置application.conf文件。

application.conf(第 1 部分):

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost/dbone?characterEncoding=UTF-8" 

db.dbtwo.driver=com.mysql.jdbc.Driver 
db.dbtwo.url="jdbc:mysql://localhost/dbtwo?characterEncoding=UTF-8" 


尝试 1 失败:两个类都保存到数据库 1 (dbone):
application.conf(第 2 部分):

ebean.default="*"
ebean.dbtwo="models.TestTwo"


尝试 2 失败:尝试保存内容时出现错误:

[PersistenceException: The type [class models.TestTwo] 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().]

application.conf(第 2 部分):

ebean.default="models.Test"
ebean.dbtwo="models.TestTwo"


如何设置以便将 Test 对象保存到 dbone 并将 TestTwo 对象保存到 dbtwo?


编辑:按要求提供 TestTwo 类(没什么特别的;我没有手动分配 Ebean 服务器,除了在 application.conf 文件中):

package models;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.validation.Constraints.Required;

@Entity
public class TestTwo extends Model{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Required
    public String testString;

    public static Model.Finder<Long, TestTwo> find = new Model.Finder<Long, TestTwo>(Long.class, TestTwo.class);

    public static TestTwo create (String testString){
        TestTwo test = new TestTwo();
        test.testString = testString;
        test.save();
        return test;
    }
}

【问题讨论】:

  • 请显示更多代码,尤其是用于保存实体的代码。
  • 配置看起来不错,可能是您的代码中的某些内容。我不完全确定您是否可以为每个类指定数据库,我只将它用于包。你可能想试试这个。

标签: java mysql playframework ebean


【解决方案1】:

进化脚本是你写的吗?如果没有,你需要在conf&gt;evolutions&gt;default&gt;1.sql写一个

创建表'testtwo':

create table testtwo(
    id          bigint auto_increment not null,
    testString      varchar(100) not null,
    constraint t1_testTwo primary key(id)
);

进一步:

SET FOREIGN_KEY_CHECKS=0;

drop table if exists testto;

SET FOREIGN_KEY_CHECKS=1;

当您刷新浏览器时,play 会要求“应用进化”,这将在您的数据库中创建“testtwo”表,您可以在其中保存实体。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,这个问题是我在任何地方都能找到的唯一相关主题。我为解决它所做的不是很漂亮,但我让我所有的模型类都属于非默认 ebean 服务器的一部分,覆盖了save()-、delete()- 和 update()-方法。这些覆盖分别调用super.save(dbname)super.delete(dbname)super.update(dbname),其中dbname 是ebean 服务器的名称。

    在启动时,我从配置中提取名称并保存它们,这样它们就不会被硬编码。虽然看起来很多余,但它为我解决了问题。我希望它可以帮助其他像我一样在多年后徘徊在这个问题上的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多