Unable to use multiple ebean databases in Play 2

2019-04-20 02:05发布

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

5条回答
姐就是有狂的资本
2楼-- · 2019-04-20 02:22

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

classA.save("firstDB");
查看更多
forever°为你锁心
3楼-- · 2019-04-20 02:25

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

查看更多
叼着烟拽天下
4楼-- · 2019-04-20 02:26

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()"

查看更多
The star\"
5楼-- · 2019-04-20 02:28

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!!)
查看更多
疯言疯语
6楼-- · 2019-04-20 02:42

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(); 
查看更多
登录 后发表回答