2013-07-29 58 views
1

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

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:尝试保存某些内容时出现错误NG:

[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" 


我如何设定让测试对象被保存到dbone和TestTwo对象dbtwo?


编辑:TestTwo类的要求(没有什么特别的,我没有手动比application.conf文件分配Ebean服务器,其他):

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; 
    } 
} 
+0

请再展示一些代码,特别是用于保存实体的代码。 –

+0

配置看起来不错,它可能是你的代码中的东西。我不完全确定你是否可以为每个类指定数据库,我只用它作为包。你可能想试试这个。 – nylund

+0

增加了TestTwo类。 –

回答

0

我有同样的问题,这问题是我可以在任何地方找到的唯一相关主题。我所做的解决这个问题的做法并不是很好,但是我将所有属于非默认ebean服务器的模型类覆盖了save() - ,delete()update()-方法。这些覆盖分别称为super.save(dbname),super.delete(dbname)super.update(dbname),其中dbname是ebean服务器的名称。

在启动时,我将名称从配置中取出并保存,以便它们不是硬编码的。尽管看起来很冗余,但它为我解决了这个问题。我希望它能帮助像我一样徘徊在这个问题后的其他人!

0

您是否编写了进化脚本?如果没有,你需要写一个在conf>evolutions>default>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; 

,玩会问到 “应用进化”,这将在您的数据库中创建'testtwo'表,您可以在其中保存实体。

相关问题