How to configure 2 MySQL databases in Play Framework?

advertisements

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


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.