除了defaultTargetDataSource之外,AbstractRoutingDataSource + JPA不会创建表 [英] AbstractRoutingDataSource + JPA won't create table except defaultTargetDataSource

查看:433
本文介绍了除了defaultTargetDataSource之外,AbstractRoutingDataSource + JPA不会创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA批注(Hibernate实现)来初始化我的数据库模式.然后,我按照文章动态数据源路由来实现动态数据源路由类.

I use JPA annotations (Hibernate implementation) to initialize my DB schema. And i follow the article DYNAMIC DATASOURCE ROUTING to implement the dynamic datasource routing class.

但是,我有两个数据库(映射2个数据源).我将第一个数据源设置为defaultTargetDataSource.然后启动我的应用程序.当我的应用程序尝试访问第二个数据源时,它告诉我该表不存在.看来AbstractRoutingDataSource只为默认数据源创建表,而为其他数据源创建表.

However, i have two databases (mapped 2 data sources). I set the first data source as defaultTargetDataSource. then start my application. When my application try to access 2nd data source, it tell me the table doesn't exist. It seems AbstractRoutingDataSource only create the table for the default data source but other data sources.

有没有在所有数据库中创建架构的想法?

Is there any idea to create schema in all databases ?

PS.我正在使用AbstractRoutingDataSource来实现自己的数据库分片.

PS.I'm using AbstractRoutingDataSource to implement my own DB shards.

推荐答案

我猜您正在使用hibenate配置:

I guess that you are using the hibenate configuration:

spring:
  jpa:
    hibernate:
      ddl-auto: update

反映实体对数据库架构的更改.只要我们使用配置为在启动时连接的单个数据源,此方法就可以正常工作.

to reflect the entity changes to the database schema. This works fine as long as we use a single data source that is configured to be connected at startup.

但是,如果您有多个数据源,则无法使用此功能. AbstractRoutingDataSource的一般方法是在启动时没有数据源,而在运行时选择它.

However, if you have multiple data sources it is not possible to use this feature. The general approach with AbstractRoutingDataSource is to not have a data source at startup but select it at runtime.

如果选择一个主数据源,则它将仅应用于主数据源,因为休眠模式在启动时会应用此功能,但是其余数据库将不会迁移.

If you select a primary data source, then it will be only applied to the primary one as hibernates applies this feature at startup, but the remaining databases will not be migrated.

要反映所有数据库的更改,可以使用Flyway或Liquibase之类的数据库迁移工具.

To reflect the changes to all of your databases you can use a database migration tool such as Flyway or Liquibase.

Flyway正在使用SQL,并且非常易于配置和使用.

Flyway is using SQL and pretty easy to configure and use to use.

这篇关于除了defaultTargetDataSource之外,AbstractRoutingDataSource + JPA不会创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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