具有来自数据库的模式的Spring Data JPA存储库的动态模式选择

时间:2022-09-11 12:58:31

as the title says, I have an application (Spring Boot) which must execute some queries on specified objects on a SQL Server database. Such database having a single catalog and multiple schemas, which must be selected based on a previous query and some command line parameters.

正如标题所说,我有一个应用程序(Spring Boot),它必须对SQL Server数据库上的指定对象执行一些查询。此类数据库具有单个目录和多个模式,必须根据先前的查询和一些命令行参数进行选择。

I cannot seem to implement a strategy which involves Hibernate multi-tenancy, as most of the tutorials and answers on this site deal with schema names coming from a web request or an external parameter, while I need a database connection before creating the main multi-tenant EntityManager. So, I switched to a custom DataSource which tries to change the connection's default schema (using ALTER USER... WITH DEFAULT_SCHEMA = ...). But this also fails because the logged-in user does not have permission to alter his own default schema.

我似乎无法实现涉及Hibernate多租户的策略,因为此站点上的大多数教程和答案都处理来自Web请求或外部参数的模式名称,而我在创建主要多个数据库之前需要数据库连接租户EntityManager。所以,我切换到一个自定义DataSource,它试图改变连接的默认模式(使用ALTER USER ... WITH DEFAULT_SCHEMA = ...)。但这也失败了,因为登录用户无权更改自己的默认架构。

So I'm at a loss of what to do. Any suggestions?

所以我不知所措。有什么建议?

1 个解决方案

#1


1  

Just create an EntityManager(Factory) per schema and put them in a map to choose from.

只需为每个模式创建一个EntityManager(Factory),并将它们放在一个地图中即可供选择。

If you don't know the schemas before hand you can create EntityManager(Factory)s as soon as you learn about a schema.

如果您事先不知道模式,则可以在了解模式后立即创建EntityManager(Factory)。

that you can configure an EntityManagerFactory programatically.

您可以以编程方式配置EntityManagerFactory。

#1


1  

Just create an EntityManager(Factory) per schema and put them in a map to choose from.

只需为每个模式创建一个EntityManager(Factory),并将它们放在一个地图中即可供选择。

If you don't know the schemas before hand you can create EntityManager(Factory)s as soon as you learn about a schema.

如果您事先不知道模式,则可以在了解模式后立即创建EntityManager(Factory)。

that you can configure an EntityManagerFactory programatically.

您可以以编程方式配置EntityManagerFactory。