以编程方式从会话工厂获取休眠默认架构名称?
我想知道是否有办法从会话工厂获取默认模式名称?我需要获取它的原因是因为我必须使用一个本机 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚发现 hibernate 有 {h-schema} 替换,可以在本机 sql 查询中使用。因此,当您连接到 Oracle 数据库中的一个模式并希望针对不同的模式执行查询时,这可以干净地完成这项工作。示例是:
这种方式不是在查询中执行手动
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:
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.在使用 Criteria api 的 Restrictions.sqlRestriction(...) 时,我对 John 的使用 {h-schema} 的解决方案遇到了问题(可能是因为此替换发生在单独的 HQL api 内)。与迈克尔的解决方案类似,我使用了:
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:
这就能解决问题:
This will do the trick:
@Ryan Morlok,由于缺乏声誉,我无法评论你的答案。您的答案是正确的,但现在已弃用。在 hibernate 5.0.1 版本中,模式名称可以使用
@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