无法在Play 2中使用多个ebean数据库



我们正在使用Play Framework 2.0.3设置一个稍微复杂的项目。

我们需要访问几个数据库(预先存在的),并希望使用内置的框架(即EBean)来访问。

我们试图在"models"包中创建所有模型类,然后将每个类及其FQN映射到应用程序中相应的EBean属性。conf:

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

这似乎不起作用:

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] 

我们检查了又检查,配置还可以!

然后,我们尝试为每个数据库模型类使用不同的Java包,并在应用程序中相应地映射它们。conf:

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

当从数据库中读取信息时,这很好,但当您尝试保存/更新对象时,我们会得到:

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

有什么想法吗?

谢谢!Ricardo

您必须在查询中指定要访问的数据库。

例如,如果您想从secondDB:中检索所有用户

// Get access to your secondDB
EbeanServer secondDB = Ebean.getServer("secondDB");
// Get all users in secondDB
List<User> userList = secondDB.find(User.class).findList(); 

使用save()delete()update()refresh()时,必须指定Ebean服务器,例如save()方法:

classA.save("firstDB");

我也遇到过同样的问题,花了一整天的时间去调查,终于找到了。

1.定义命名eabean服务器

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

现在您在运行时有两个ebean服务器[default]和[aux]。

2.应用程序配置文件ebean.default="models.*"

ebean.aux= "secondary.*"

现在,软件包模型下的实体。*配置为[默认]服务器,软件包辅助下的实体配置为[辅助]服务器。我认为这可能与java类增强有关。您不需要将实体分离到不同的包中,但如果不同ebean服务器的实体在同一个包中,可能会导致奇怪的麻烦和异常。

  1. 当使用您的模型时,保存/删除/更新相关方法应添加服务器名称作为参数

    学生s=新学生();s.save("aux");

  2. 当使用finder时,您应该将您的finder定义为

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

情况可能不同,我用Play 2.1.0运行了这个SomeClass未增强的PersistenceException,缺少的只是SomeClass模型类中的public声明,我已经忘记了。。

在Play 2.1.0中,错误消息有点不同:

PersistenceException: java.lang.IllegalStateException: Class [class play.db.ebean.Model] is enhanced and [class models.Address] is not - (you can not mix!!)

这解决了我保存到数据库表并解决错误的问题:

"javax.persistence.PersistenceException:默认的EbeanServer尚未定义?这通常是通过ebean.datasource.default属性设置的。否则,它应该通过registerServer()"以编程方式注册

最新更新