多个数据库与Play Framework 2.1.x [英] Multiple Databases with Play Framework 2.1.x

查看:595
本文介绍了多个数据库与Play Framework 2.1.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据库,我需要连接到。我可以很容易地连接到他们在application.conf文件像这样:

I have 2 databases that I need to connect to. I can easily connect to them in the application.conf file like so:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/db1"
db.default.user=postgres
db.default.password="password"

db.secondary.driver=org.postgresql.Driver
db.secondary.url="jdbc:postgresql://localhost/db2"
db.secondary.user=postgres
db.secondary.password="password"

ebean.default="models.db1.*"
ebean.secondary="models.db2.*"

我在这些包中有我的模型类,它的DDL正确生成表。

I have my model classes in those packages, and it DDL generates the tables properly.

问题在于实际使用这些实体。任何不在default包中的东西都会抛出此错误(使用辅助数据库中的Users表作为示例)

The problem lies in actually working with these entities. Anything not in the "default" package throws this error (using the Users table in the secondary database as an example)

如果我尝试查询表的所有行:

If I try to query all the rows of the table:

List<Users> users = Users.find.all();

它会引发此错误:

[PersistenceException: models.db2.Users is NOT an Entity Bean registered with this server?]

即使我100%确定用户表在后端,它是一个注册表,DDL工作,使这个表正确,我正在导入正确的类。

Even though I am 100% sure that the Users table is there in the backend, it is a registered table the DDL works and makes this table properly, and I am importing the proper classes.

是否有一定的方式需要查询不在默认包中的模型类?

Is there a certain way I need to query model classes that aren't in the default package?

编辑:我意识到堆栈跟踪显示它试图使用DefaultServer。如何使用辅助服务器?

I realize that the stack trace shows that it's trying to use the DefaultServer. How can I make it use the secondary server?

    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:989) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:946) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.find(DefaultServer.java:982) ~[avaje-ebeanorm-server.jar:na]
    at play.db.ebean.Model$Finder.all(Model.java:254) ~[play-java-ebean_2.10.jar:2.1.3]


推荐答案

确定,您的 application.conf 似乎是正确的。

Ok, your application.conf seems to be correct.

您可以使用这样的辅助服务器:

You may use the secondary server like this:

EbeanServer secondary = Ebean.getServer("secondary");
secondary.find(User.class).findList();

一旦你有你的辅助服务器,你可以像对待 Ebean 单例。

Once you've got your secondary server, you may treat it just as you treat the Ebean singleton.

这篇关于多个数据库与Play Framework 2.1.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆