JPQL 到 SQL 转换器
是否可以访问JPQL生成的SQL?
我想稍后使用 SQL 创建一个用于我的目的的视图。
如果有必要的话我正在使用 Hibernate。
Is it possible to access the SQL which is generated by JPQL?
I would like to use the SQL to later create a view for my purposes.
I am using Hibernate if it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从 Hibernate
Interceptor
访问 SQLString
,更准确地说是从方法Interceptor.html#onPrepareStatement(java.lang.String)
如果您决定采用这种方式,最好的选择是扩展
EmptyInterceptor
并仅覆盖您想要的方法。您可以在
persistence.xml
中使用以下声明来挂钩拦截器:You can access the SQL
String
from an HibernateInterceptor
, more precisely from the methodInterceptor.html#onPrepareStatement(java.lang.String)
If you decide to go this way, the best option is to extends
EmptyInterceptor
and to override only the methods you want.You can hook your interceptor using the following declaration in your
persistence.xml
:你可以将
hibernate.show_sql
属性设置为true,那么所有的SQL都会显示在控制台上。You can set the
hibernate.show_sql
property to true, then all the SQL will be shown on the console.因此,您需要编写自己的 JPQL 实现 :-) 毕竟,将 JPQL 转换为 RDBMS 数据存储的 SQL 就是 JPQL 实现。祝你好运!
当然,您可以利用现有的 JPQL 实现,无论是 Hibernate、EclipseLink 还是 DataNucleus...并且由于它们都是开源的,因此您应该深入研究它们的代码库。
So you need to write your own JPQL implementation then :-) After all, converting JPQL into SQL for an RDBMS datastore is what a JPQL implementation is. Good Luck!
Sure you could make use of an existing JPQL implementation though, whether Hibernate, or EclipseLink, or DataNucleus ... and since they are all open source you ought to go and dig in their codebase.