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

查看:27
本文介绍了使用 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.

问题在于实际使用这些实体.任何不在默认"包中的东西都会抛出这个错误(以辅助数据库中的用户表为例)

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% 确定 Users 表在后端,但它是一个注册表,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]

推荐答案

我遇到了同样的问题,我通过在 Model.Finder 级别指定服务器名称来修复它.

I had the same problem and I fixed it by specifying the server name at the Model.Finder level.

所以在你的情况下,在你的 User 类中,你应该有类似的东西:

So in your case, in your User class, you should have something like :

public static Model.Finder<Long, User> find = new Finder<Long, User>("secondary", Long.class, User.class);

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

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