以编程方式从会话工厂获取休眠默认架构名称?

发布于 2024-10-14 19:56:04 字数 112 浏览 3 评论 0原文

我想知道是否有办法从会话工厂获取默认模式名称?我需要获取它的原因是因为我必须使用一个本机 SQL,并且我有多个用于多个模式的会话工厂和一个数据源。所有生成的休眠查询都由对其他模式具有选择访问权限的单个用户运行。

I was wondering if there is a way to get the default schema name from session factory, somehow? The reason I need to get it is because I have to use a one native SQL and I have multiple session factories for multiple schemas and a single data source. All the generated hibernate queries are being ran by a single user which has select access to other schemas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思故 2024-10-21 19:56:04

我刚刚发现 hibernate 有 {h-schema} 替换,可以在本机 sql 查询中使用。因此,当您连接到 Oracle 数据库中的一个模式并希望针对不同的模式执行查询时,这可以干净地完成这项工作。示例是:

select * from {h-schema}table_name

这种方式不是在查询中执行手动 replaceAll 操作,而是考虑到每个会话工厂都配置有 "hibernate.default_schema" 属性,hibernate 将处理所有事情。

I just found out that hibernate has {h-schema} replacement that can be used in native sql queries. So this does the job cleanly when you are connected to a one schema in oracle database and want to execute queries against different schemas. Example would be:

select * from {h-schema}table_name

This ways instead of doing a manual replaceAll in a query, hibernate will take care of everything given that each session factory is configured with "hibernate.default_schema" property.

御弟哥哥 2024-10-21 19:56:04

在使用 Criteria api 的 Restrictions.sqlRestriction(...) 时,我对 John 的使用 {h-schema} 的解决方案遇到了问题(可能是因为此替换发生在单独的 HQL api 内)。与迈克尔的解决方案类似,我使用了:

SessionFactoryImplementor sfi = (SessionFactoryImplementor)sessionFactory;
String name = sfi.getSettings().getDefaultSchemaName();

I had problems with John's solution to use {h-schema} when using the Criteria api's Restrictions.sqlRestriction(...) (probably because this substitution happens within the separate HQL api). Similar to Michael's solution, I used:

SessionFactoryImplementor sfi = (SessionFactoryImplementor)sessionFactory;
String name = sfi.getSettings().getDefaultSchemaName();
夏花。依旧 2024-10-21 19:56:04

这就能解决问题:

  SessionFactoryImplementor sfi = (SessionFactoryImplementor) getSessionFactory();           
  Settings settings = sfi.getSettings();
  ConnectionProvider connectionProvider = settings.getConnectionProvider();
  try {
        Connection connection = connectionProvider.getConnection();
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String url = databaseMetaData.getURL();
        //substring the string to what you want
        System.out.println(url);
  } catch (SQLException e) {
       //throw something
  }

This will do the trick:

  SessionFactoryImplementor sfi = (SessionFactoryImplementor) getSessionFactory();           
  Settings settings = sfi.getSettings();
  ConnectionProvider connectionProvider = settings.getConnectionProvider();
  try {
        Connection connection = connectionProvider.getConnection();
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String url = databaseMetaData.getURL();
        //substring the string to what you want
        System.out.println(url);
  } catch (SQLException e) {
       //throw something
  }
百思不得你姐 2024-10-21 19:56:04

@Ryan Morlok,由于缺乏声誉,我无法评论你的答案。您的答案是正确的,但现在已弃用。在 hibernate 5.0.1 版本中,模式名称可以使用

(SessionFactoryImpl)sessionFactory).getProperties().getProperty("hibernate.default_schema")

@Ryan Morlok, I can not comment on your answer due to lack of reputation. Your answer is correct, but its deprecated now. In hibernate version 5.0.1, schema name can be get using

(SessionFactoryImpl)sessionFactory).getProperties().getProperty("hibernate.default_schema")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文