[英]How do I set up 2 MySQL databases in Play Framework?
I need to set up 2 separate databases. 我需要设置2个单独的数据库。 One for class Test and the other for class TestTwo, but I don't know how to configure the application.conf file. 一个用于Test类,另一个用于TestTwo类,但是我不知道如何配置application.conf文件。
application.conf (part 1): 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"
Failed attempt 1: both classes get saved to database 1 (dbone): 尝试1失败:两个类都保存到数据库1(dbone)中:
application.conf (part 2): application.conf(第2部分):
ebean.default="*"
ebean.dbtwo="models.TestTwo"
Failed attempt 2: I get an error when trying to save something: 尝试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 (part 2): application.conf(第2部分):
ebean.default="models.Test"
ebean.dbtwo="models.TestTwo"
How can I set things up so Test objects are saved to dbone and TestTwo objects to dbtwo? 我该如何设置以便将Test对象保存到dbone并将TestTwo对象保存到dbtwo?
EDIT: TestTwo class as requested (nothing special; I didn't assign an Ebean server manually, other than in the application.conf file): 编辑: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;
}
}
I had the same issue and this question was the only relevant topic I could find about it anywhere. 我遇到了同样的问题,这个问题是我在任何地方都可以找到的唯一相关主题。 What I did to resolve it was not very pretty, but I made all of my model classes that are part of the non-default ebean server override the save()
-, delete()
- and update()
-methods. 解决该问题的方法不是很漂亮,但是我使属于非默认ebean服务器的所有模型类都覆盖了save()
-, delete()
-和update()
方法。 These overrides respectively call super.save(dbname)
, super.delete(dbname)
and super.update(dbname)
, where dbname
is the name of the ebean server. 这些覆盖分别调用super.save(dbname)
, super.delete(dbname)
和super.update(dbname)
,其中dbname
是ebean服务器的名称。
On startup, I pull the names out of the config and save them so they aren't hard-coded. 启动时,我将名称从配置中拉出并保存,以免被硬编码。 Although it seems very redundant, it fixed the problem for me. 尽管它看起来很多余,但对我来说却解决了这个问题。 I hope it helps someone else who wanders into this issue years later like I did! 我希望它可以帮助像我一样在几年后迷失于这个问题的其他人!
Did you write the evolution script? 您是否编写了演化脚本? If not you need to write one in conf>evolutions>default>1.sql
如果不是,则需要在conf>evolutions>default>1.sql
编写一个
Creating table 'testtwo': 创建表“ testtwo”:
create table testtwo(
id bigint auto_increment not null,
testString varchar(100) not null,
constraint t1_testTwo primary key(id)
);
Further: 进一步:
SET FOREIGN_KEY_CHECKS=0;
drop table if exists testto;
SET FOREIGN_KEY_CHECKS=1;
When you refresh the browser, play will ask to "apply evolution", this will create 'testtwo' table in your DB where you can save entities. 刷新浏览器时,播放将要求“应用进化”,这将在数据库中创建“ testtwo”表,您可以在其中保存实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.