简体   繁体   中英

How do I set up 2 MySQL databases in Play Framework?

I need to set up 2 separate databases. One for class Test and the other for class TestTwo, but I don't know how to configure the application.conf file.

application.conf (part 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):
application.conf (part 2):

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


Failed attempt 2: I get an error when trying to save something:

[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):

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?


EDIT: TestTwo class as requested (nothing special; I didn't assign an Ebean server manually, other than in the application.conf file):

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. These overrides respectively call super.save(dbname) , super.delete(dbname) and super.update(dbname) , where dbname is the name of the ebean server.

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

Creating table '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM