我需要设置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;
}
}
请再展示一些代码,特别是用于保存实体的代码。 –
配置看起来不错,它可能是你的代码中的东西。我不完全确定你是否可以为每个类指定数据库,我只用它作为包。你可能想试试这个。 – nylund
增加了TestTwo类。 –