无法在Play 2中使用多个ebean数据库
我们正在使用Play Framework 2.0.3建立一个稍微复杂的项目。
我们需要访问几个数据库(预先存在),并希望使用框架内置工具(即EBean)来实现。
我们尝试在“models”包中创建所有模型类,然后将每个类的FQN映射到application.conf中相应的EBean属性:
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包,并在application.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()
有任何想法吗?
谢谢! 里卡多
您必须在查询中指定要访问的数据库。
例如,如果要从secondDB检索所有用户:
// Get access to your secondDB EbeanServer secondDB = Ebean.getServer("secondDB"); // Get all users in secondDB List userList = secondDB.find(User.class).findList();
使用save()
, delete()
, update()
或refresh()
,必须指定Ebean服务器,例如save()方法:
classA.save("firstDB");
我遇到了同样的问题,浪费了一整天来调查它,最后我得到了它。
1.define命名为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服务器[默认]和[aux]。
2.app conf file ebean.default =“models。*”
ebean.aux= "secondary.*"
现在在包模型下运行。*配置为[默认]服务器和包二级下的实体。*配置为[aux]服务器。 我认为这可能与java类增强有关。 您不需要将实体分成不同的包,但如果不同的ebean服务器的实体在同一个包中,则可能会导致奇怪的麻烦和exception。
-
使用模型时,保存/删除/更新相关方法应添加服务器名称作为参数
学生s =新学生(); s.save( “AUX”);
-
使用finder时,您应该将finder定义为
public static Finder find = new Finder(“aux”,Long.class,Student.class);
可能不是同一个案例,我跑到这个SomeClass没有使用Play 2.1.0 增强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!!)
这解决了我保存到我的db表并解决错误的问题:
“javax.persistence.PersistenceException:默认的EbeanServer尚未定义?这通常是通过ebean.datasource.default属性设置的。否则它应该通过registerServer()以编程方式注册”