如何拦截Hibernate生成的SQL? [英] How to intercept the Hibernate generated SQL?

查看:609
本文介绍了如何拦截Hibernate生成的SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个像兄弟一样工作的安全系统(像监视强制访问控制),我们必须拦截并处理hibernate产生的所有选择语句。我们将用户,时间戳和sql select存储在数据库中,以便使用其他工具进行分析。这些信息可以确定用户试图查看的内容。对于select语句,准备好的属性是有价值的。我们需要包含所有参数的完整的SQL语句。



有没有我们可以加入和处理所有这些事情的监听器或拦截器?到目前为止最大的问题是收集语句参数。

谢谢

解决方案

div>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆