java 如何在查询时设置休眠实体使用的模式名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/398215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set the schema name used by hibernate entities at query time?
提问by Chris R
Our application uses Hibernate for ORM, and stores data in several schemas, accessing them with a user whose grants are customized for the application.
我们的应用程序将 Hibernate 用于 ORM,并将数据存储在多个模式中,并通过为应用程序定制授权的用户访问它们。
The schema names are determined at runtime based on data; it's not feasible to include their names in the entity mapping documents. This means that I need a way to tell Hibernate to use a specific schema name when performing lookups. Is there a way to do this?
模式名称是在运行时根据数据确定的;在实体映射文档中包含他们的名字是不可行的。这意味着我需要一种方法来告诉 Hibernate 在执行查找时使用特定的模式名称。有没有办法做到这一点?
采纳答案by Robert Simmons
Here's a page that lists some ways you can manage multiple schemas in Hibernate. I'd probably go with implementing your own connection provider. You'll probably want to disable caching as well.
这是一个页面,列出了您可以在 Hibernate 中管理多个模式的一些方法。我可能会去实现你自己的连接提供程序。您可能还想禁用缓存。
回答by paulmurray
We ran into this problem at work. I fixed it, as Robert suggests, by creating a connection provider (ie, an implementation of DataSource), called ""OracleSchemaRemappingDataSource" and using spring to do the plumbing.
我们在工作中遇到了这个问题。正如罗伯特建议的那样,我通过创建一个名为“OracleSchemaRemappingDataSource”的连接提供程序(即 DataSource 的实现)并使用 spring 进行管道连接来修复它。
Basically, this datasource implements getConnection(). The implementation of that method works by getting a connection from some other data source by spring injection, which it assumes to be an oracle connection, and then executing
基本上,这个数据源实现了 getConnection()。该方法的实现是通过 spring 注入从其他一些数据源获取连接,它假定是一个 oracle 连接,然后执行
ALTER SESSION SET CURRENT_SCHEMA = 'someotherschema'
and them passing that connection back.
然后他们将那个连接传回来。
All of the hibernate config is careful to use names without specifying schemas for them.
所有的 hibernate 配置都小心地使用名称而不为它们指定模式。
Also: with this, you don't want to disable caching - allow hibernate to manage connections as normal, as we are not doing any magic within the app such as using different connections on a per-user basis.
另外:有了这个,您不想禁用缓存 - 允许 hibernate 正常管理连接,因为我们没有在应用程序中做任何魔术,例如在每个用户的基础上使用不同的连接。

