简体   繁体   English

如何在 ebean 中使用多个数据库

[英]How to use multiple databases with ebean

I'm trying to use Ebean framework ORM queries to different postgres databases.我正在尝试使用 Ebean 框架 ORM 查询不同的 postgres 数据库。 Following these and these docs, queries to my single database works fine.按照这些这些文档,对我的单个数据库的查询工作正常。 But to address another one, I have to explicitly call it by DB.byName("name").但要解决另一个问题,我必须通过 DB.byName("name") 显式调用它。 How can I avoid this explicit call?如何避免这种显式调用?

To be clear, first example - as it is, second - as I want it to be:为了清楚起见,第一个例子——事实上,第二个——我希望它是:

  1. QDbEmailQueue(DB.byName("superadmin")).findCount() works QDbEmailQueue(DB.byName("superadmin")).findCount()有效

  2. QDbEmailQueue().findCount() throws next exception: QDbEmailQueue().findCount()抛出下一个异常:

 Query threw SQLException:ERROR: relation "email_queue" does not exist Position: 22 Bind values:[] Query was:select count(*) from email_queue t0 javax.persistence.PersistenceException: Query threw SQLException:ERROR: relation "email_queue" does not exist Position: 22 Bind values:[] Query was:select count(*) from email_queue t0

In both cases I have my entity-class configured like that in kotlin:在这两种情况下,我都像在 kotlin 中那样配置了我的实体类:

@Entity
@DbName(value = "superadmin")
@Table(name = "email_queue")
class DbEmailQueue(
    @Id
    val emailQueueId: Int
    ...
): Model("superadmin")

And in both cases there is same configuration with EbeanConfigProvider.java like here :在这两种情况下,EbeanConfigProvider.java 的配置相同,如下所示

public class EbeanConfigProvider implements ServerConfigProvider {

    public static final String SUPERADMIN = "superadmin";
    public static final String DB = "db";

    @Override
    public void apply(ServerConfig serverConfig) {

        DataSourceConfig dataSourceConfig = serverConfig.getDataSourceConfig();
        dataSourceConfig
                .setUsername(Config.INSTANCE.getAppDbUserName())
                .setPassword(Config.INSTANCE.getAppDbUserPswd());

        switch (serverConfig.getName()) {
            case DB: {
                serverConfig.setDefaultServer(true);
                dataSourceConfig.setUrl(Config.INSTANCE.getAppDbHostUrl() + Config.INSTANCE.getAppDbName());
                break;
            }
            case SUPERADMIN: {
                serverConfig.setDefaultServer(false);
                dataSourceConfig.setUrl(Config.INSTANCE.getAppDbHostUrl() + Config.INSTANCE.getSuperadminDbName());
                break;
            }
        }
    }
}

and application.yaml according to this doc :和 application.yaml 根据此文档

ebean:
  dbSchema: public  # use this schema rather than public
  migration:
    run: false       # run database migrations on startup
  search:
    packages: om.bo.mypackage.db
    querybean-packages: om.bo.mypackage.db.query

datasource:
  db:
    driver: org.postgresql.Driver
    default: true

  superadmin:
    driver: org.postgresql.Driver
    dbName: superadmin
    default: false

Just update ebean version.只需更新 ebean 版本。 I had ebean 12.1.10.我有 ebean 12.1.10。 At 12.1.11 problem still exists, but at versions 12.1.12 and 12.1.13 everything works as I want (starting from 12.2.1 problem appears aggain with different exception)在 12.1.11 问题仍然存在,但在 12.1.12 和 12.1.13 版本中一切正常(从 12.2.1 开始,问题再次出现,但有不同的例外)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM