运行时使用JPA在PostgreSQL中切换架构 [英] Runtime switch the schema in PostgreSQL using JPA
问题描述
我正在使用JPA管理PostgreSQL数据库. DB中存在多个模式.由于产品要求,我们需要根据输入请求从不同的架构中获取数据.我设法从公共架构中获取数据.
I am using JPA to manage the PostgreSQL database. Multiple schemas are present in DB. Due to product requirement we need fetch the data from different schemas as per input request. I managed to fetch the data from public schema.
但是我找不到配置JPA的解决方案,以便它可以在运行时切换模式并获取数据.
But I couldn't find a solution to configure the JPA so that it will switch the Schemas and fetch the data at run time.
推荐答案
您有两个选择.
如果各表的表名不同,最简单的方法是为用户设置默认的search_path
:
If the table names are different across schemas, the easiest way is to setup a default search_path
for the user:
alter user your_user set search_path = public,schema_one,schema_two,schema_four;
完成此操作后,将沿着search_path中的模式搜索表.
Once you have done that, tables are searched along the schemas in the search_path.
再次:如果您没有重复的表名,这将仅有效.
Again: this will only work if you do not have duplicate table names.
另一种选择是在运行查询之前动态更改架构,例如:
The other option is to change the schema dynamically before running the query, e.g.:
set schema 'schema_one';
这篇关于运行时使用JPA在PostgreSQL中切换架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!