javaplayframeworkplayframework-2.0ebean

Play : bean class is not enhanced, using EbeanServerFactory


using play framework 2.4.2, I have to connect to a distant database wich can be offline.

I am trying to avoid the "Cannot connect to database [distantdatabase]" exception at startup.

After trying lots of methods, I decided to create manually an EbeanServerFactory to access this distant database.

This is my model :

@Entity
@Table(name="vehicules")
public class Vehicule {
    @Id
    public Long id;
    public Long noParc;
    public Long etat;
}

And this is how I try to access data

// Classes of distant database
List<Class<?>> saetrClasses = new ArrayList<Class<?>>();
saetrClasses.add(Vehicule.class);

// Access configuration to distant database
DataSourceConfig ds = new DataSourceConfig();
ds.setDriver("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost/distantdatabase");
ds.setUsername("stack");
ds.setPassword("overflow");

ServerConfig saetrServerConfig = new ServerConfig();
saetrServerConfig.setName("distantdatabase");
saetrServerConfig.setDefaultServer(false);
saetrServerConfig.setClasses(saetrClasses);
saetrServerConfig.setRegister(false);
saetrServerConfig.setDataSourceConfig(ds);

Logger.warn("EbeanServerFactory.create...");

EbeanServer saetrServer = EbeanServerFactory.create(saetrServerConfig);

saetrServer.find(Vehicule.class).where().gt("lastMaj", lastRequestTime).findList();

build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean, PlayEnhancer)

plugins.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")

I get the following error

[warn] - application - EbeanServerFactory.create...
[error] - com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager - Error in deployment
java.lang.IllegalStateException: Bean class models.distantdatabase.Vehicule is not enhanced?
[...]

I do not understand this error. I've looked a lot of examples and did not find what i'm missing. Any idea ?

Edit : I looked into source code of ebean, and I get this error because my model does not implements EntityBean interface. How can I force implementation of this interface ?


Solution

  • I finally get it, the database model was not exactly what ebean expected.

    To solve it, I add in application.conf parameters as for a local database :

    ebean.distantdatabase="models.distantdatabase.*"
    play.evolutions.db.distantdatabase.enabled=true
    play.evolutions.db.distantdatabase.autoApply = true
    db = {
      saetr {
        driver=com.mysql.jdbc.Driver
        url="jdbc:mysql://localhost/distantdatabase"
        username=play
        password=framework
      }
    }
    

    I created an empty database "distantdatabase" on mySQl server. I launched my application once. It created tables in the database, and the tables creation sql script (/conf/evolutions/distantdatabase/1.sql). With this file, you could know exactly what database model ebean is expecting.

    Then, to use a distant database, which could be offline, you need to not declare anything about it into application.conf, and declare a ServerConfig as this :

    ServerConfig saetrServerConfig = new ServerConfig();
    saetrServerConfig.setName("distantdatabase");
    saetrServerConfig.setDdlGenerate(false); // ddlGenerate drops all tables and create new tables
    saetrServerConfig.setDdlRun(false); // ddlRun run migration scripts
    saetrServerConfig.setDefaultServer(false);
    saetrServerConfig.setRegister(true);
    saetrServerConfig.setDataSourceConfig(ds);
    saetrServerConfig.setClasses(saetrClasses);
    

    You could find example from ebean tests : https://github.com/ebean-orm/avaje-ebeanorm/blob/master/src/test/java/com/avaje/tests/basic/MainDbBoolean.java