如何拦截Hibernate生成的SQL? [英] How to intercept the Hibernate generated SQL?
问题描述
有没有我们可以加入和处理所有这些事情的监听器或拦截器?到目前为止最大的问题是收集语句参数。
谢谢
当 org.hibernate包被设置为DEBUG,并且设置了hibernate.show_sql属性。如果您想在数据库本身输出日志记录器,请使用JDBCAppender。
或者,您可以查看 log4jdbc项目,其中声明如下所示:
lockquote
在记录输出中,对于准备的
语句,绑定参数是自动插入到
中的SQL
输出。这极大地提高了
的可读性和调试许多
的情况。
如果这不合适,您可以调查无论是 P6Spy都可以在您的情况下使用。在WebLogic Server上,等效功能可通过 WebLogic JDBC Spy ,它为某些数据库提供了WebLogic JDBC驱动程序。这两个都写入到System.out而不是数据库(除非我错了),所以它可能没有那么有用。
For a security system which works as a big brother (like a watched mandatory access control), we have to intercept and handle all select statements hibernate is generating. We store the user, the timestamp and the sql select in a database to enable some profiling with other tools. The informations allow to determine what a user tried to look at. For the select statements the prepared properties are valuable. We need the complete SQL statement including all parameters.
Is there any listener or interceptor where we can join and handle all these things? The biggest outstanding problem so far is collecting of the statement parameters.
Thank you
The actual parameter values happen to be made available (atleast to my knowledge), when the logging level of the org.hibernate package is set to DEBUG, and with the hibernate.show_sql property set. Use a JDBCAppender, if you want the logger output in the database itself.
Alternatively, you could take a look at the log4jdbc project, which claims the following:
In the logged output, for prepared statements, the bind arguments are automatically inserted into the SQL output. This greatly Improves readability and debugging for many cases.
If that is not suitable, you could investigate whether P6Spy can be used in your situation. On WebLogic Server, the equivalent functionality is achieved via the WebLogic JDBC Spy, which comes out of the box with the WebLogic JDBC drivers for certain databases. Both of these write to System.out and not to a database (unless I'm mistaken), so it might not be that useful.
这篇关于如何拦截Hibernate生成的SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!