Unable to use multiple ebean databases in Play 2
Asked Answered
B

5

13

We are setting up a slightly complicated project using Play Framework 2.0.3.

We need to access several databases (pre-existing) and would like to do it using the frameworks built-in facilities (ie. EBean).

We tried to create all model classes within the "models" package, and then map each class with its FQN to the corresponding EBean property in the application.conf:

ebean.firstDB="models.ClassA,models.ClassB,models.ClassC"
ebean.secondDB="models.ClassD"
ebean.thirdDB="models.ClassE,models.ClassF"

This doesn't seem to work:

PersistenceException: Error with [models.SomeClass] It has not been enhanced but it's superClass [class play.db.ebean.Model] is? (You are not allowed to mix enhancement in a single inheritance hierarchy) marker[play.db.ebean.Model] className[models.SomeClass] 

We checked and re-checked and the configuration is OK!

We then tried to use a different Java package for each database model classes and map them accordingly in the application.conf:

ebean.firstDB = "packageA.*"
ebean.secondDB = "packageB.*"
ebean.thirdDB = "packageC.*"

This works fine when reading information from the database, but when you try to save/update objects we get:

PersistenceException: The default EbeanServer has not been defined? This is normally set via the ebean.datasource.default property. Otherwise it should be registered programatically via registerServer()

Any ideas?

Thanks! Ricardo

Benzoyl answered 30/8, 2012 at 16:15 Comment(1)
I have the exact same issue, did you find a solution ?Precedency
A
9

You have to specify in your query which database you want to access.

For example, if you want to retrieve all users from your secondDB :

// Get access to your secondDB
EbeanServer secondDB = Ebean.getServer("secondDB");

// Get all users in secondDB
List<User> userList = secondDB.find(User.class).findList(); 
Avent answered 1/7, 2013 at 8:41 Comment(1)
please extent the answer to modification and persisting of the model objectHotblooded
C
2

When using save(), delete(), update() or refresh(), you have to specify the Ebean server, for instance for the save() method:

classA.save("firstDB");
Clod answered 30/8, 2012 at 18:34 Comment(0)
H
1

I have encounter the same problem and waste a whole day to investigate into it,finally I have got it.

1.define named eabean server

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost:3306/db1"
db.default.user=root
db.default.password=123456

db.aux.driver=com.mysql.jdbc.Driver
db.aux.url="jdbc:mysql://localhost:3306/db2"
db.aux.user=root
db.aux.password=123456

now you have two ebean server [default] and [aux] at run time.

2.app conf file ebean.default="models.*"

ebean.aux= "secondary.*"

Now entiies under package models.* configured to [default] server and entities under package secondary.* configured to [aux] server. I think this may related to java class enhancement or something. You don't need to separate Entities into different packages, but if entities of different ebean servers are under same package, it may cause weird trouble and exceptions.

  1. When using you model, save/delete/update related method should add server name as parameter

    Student s = new Student(); s.save("aux");

  2. When use finder,you should define your finder as

    public static Finder find = new Finder("aux",Long.class,Student.class);

Haggadah answered 28/8, 2016 at 16:49 Comment(0)
J
0

Might not be the same case, I ran to this SomeClass not enhanced PersistenceException with Play 2.1.0, and only what was missing was a public declaration in SomeClass model class that I had forgotten..

In Play 2.1.0 the error message was a little different:

PersistenceException: java.lang.IllegalStateException: Class [class play.db.ebean.Model] is enhanced and [class models.Address] is not - (you can not mix!!)
Juanitajuanne answered 27/6, 2013 at 11:50 Comment(0)
C
0

This solved my issue with saving to my db table and resolving the error:

"javax.persistence.PersistenceException: The default EbeanServer has not been defined ? This is normally set via the ebean.datasource.default property. Otherwise it should be registered programatically via registerServer()"

Carpophagous answered 10/8, 2013 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.