2
votes

Using Play! Framework 2.0.4 and EBean as the persistence layer I am attempting to wrap a databases "meta" information to Java classes.

I map the classes in application.conf:

db.myData.url="jdbc:sqlserver://localhost:2301;databaseName=myData;"
ebean.myData="models.database.JavaTable"

I then created a class as follows:

package models.database;
import javax.persistence.Column;
import javax.persistence.Entity;

import javax.persistence.Table;

import play.db.ebean.Model;
@Entity
@Table(name="tables",schema="INFORMATION_SCHEMA",catalog="myData")
public class JavaTable extends Model{

@Column(name="TABLE_NAME")
public String table_name;

public static Finder<String, JavaTable> find = new Finder<String, JavaTable>(
    String.class, JavaTable.class
);
}

When I fire up Play!, it tells me that I need to run an evolution on the database to create the table "myData.INFORMATION_SCHEMA.tables". I then tried to test the connection via a unit test...

@Test
public void testGetTables(){
     running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
                EbeanServer server = Ebean.getServer("myData");

                List<SqlRow> rows = server.createSqlQuery("select * from myData.Information_Schema.Tables").findList();
               for(SqlRow row: rows)
                System.out.println("====>Row: " + row.toString());

            }
        });
}

The unit test executes correctly and the table names are printed out successfully.

Edit: per @nico_ekito I removed the evolution plugin in the configuration file and started getting:

RuntimeException DataSource user is null 

So, researching a bit I decided to disable other datasources in the config file and moved the database I'm attempting to communicate with to "db.default" and "ebean.default" and the models started working. Is there a way to tell a model which datasource it should use if Play has multiple databases defined? Setting the classes via "ebean.default="myData.JavaTable" did not seem to work.

2

2 Answers

2
votes

You should try to disable the evolutions by adding the following in your application.conf file:

evolutionplugin=disabled

The tests are ok because since you're not starting a real application, but using a fakeApplication() which does not use the evolutions plugin.

0
votes

Anecdotally, I've only had success defining my eBean server using package level values (so, "myData.*" instead of "myData.JavaTable"). To pull this off, you may have to move all classes for a particular eBean server to their own package.