使用来自数据库的模式为Spring Data JPA存储库动态选择模式 [英] Dynamic schema selection for Spring Data JPA repositories with schema coming from database

查看:101
本文介绍了使用来自数据库的模式为Spring Data JPA存储库动态选择模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序(Spring Boot),该应用程序必须对SQL Server数据库上的指定对象执行一些查询.这种数据库具有单个目录和多个模式,必须根据先前的查询和某些命令行参数来选择它们.

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.

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

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.

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

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

推荐答案

只需为每个架构创建一个EntityManager(Factory)并将其放置在映射中以供选择.

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.

您可以通过编程方式配置EntityManagerFactory.

that you can configure an EntityManagerFactory programatically.

这篇关于使用来自数据库的模式为Spring Data JPA存储库动态选择模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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