Hibernate 中“select 1 from DUAL”的等价物是什么?
最小的 Hibernate 查询是什么? Hibernate 是否有相当于“select 1 from DUAL”的功能?
我正在编写一个健康检查函数,并且想要执行一个最小查询,但使用 HQL 来保持代码可移植(而不是使用本机查询)。
What is the minimal Hibernate query? Is there an equivalent of "select 1 from DUAL" for Hibernate?
I am writing a healthcheck function and would like to execute a minimal query, but use HQL to keep the code portable (as opposed to using a native query).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Hibernate 方言 翻译为各种数据库,并且方言中没有“健康检查”或“验证”查询支持。这种事情通常是由连接池完成的,而不是由 Hibernate 完成的。请参见,例如 DBCP 的验证查询属性。
Hibernate's dialects are what translate to the various databases, and there's no "health check" or "validation" query support in the dialects. This kind of thing is normally done by the connection pool, not by Hibernate. See, e.g., DBCP's validation query property.
SQL
select 1 from DUAL;
不是一个很好的运行状况检查,您看到的唯一内容是服务器响应,无法判断数据本身是否运行状况良好。但是,如果您想提出这样的问题,只需创建自己的模拟 DUAL 的数据实体,并且您可以控制该对象,并且对于任何 RDBMS 都将保持通用性。不要忘记
使用 CacheMode.IGNORE 或 CacheMode.REFRESH 来强制查询。否则你可能会检查缓存而不是数据库......
The SQL
select 1 from DUAL;
is not a very good health check, the only thing you see is a server response that doesn't tell if the data itself is healthy. But if you want to make such a question, just make your own data entity that simulates DUAL and you're in control of the object and you'll stay generic for any RDBMS.Something like
Don't forget to use
CacheMode.IGNORE
orCacheMode.REFRESH
to force the query. Otherwise you might check the cache and not the database...动态本机 sql 查询将适用于它。
例如 -
session.createSQLQuery("Select 1 from Dual").uniqueResult();
您也可以使用现有表进行查询,例如
Dynamic native sql query will work for it.
for example -
session.createSQLQuery("Select 1 from dual").uniqueResult();
Also you can query with an existing table like
对于本机查询和 DUAL 表映射问题,需要使用 hibernate
而不是使用创建查询
For native queries and for DUAL table mapping problem, One need to create query with hibernate
and not with
嗯,为了可移植性,select 1 from Dual 不会移植到某些数据库,因为它们没有 Dual。 ANSI 标准 information_schema 视图将是一个选项,但同样不能保证您可能使用的所有可能的数据库都会有它们 - 但可能会更好。
Hmm, for portability, the select 1 from dual is not going to port to some DBs since they do not have dual. ANSI standard information_schema views would be an option, but there is again no guarentee that all the possible DBs you may work against will have them - but might be better odds.