Java Hibernate 显示真实的 SQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2536829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate show real SQL
提问by Tommaso Taruffi
if I set
如果我设置
<property name="show_sql">true</property>
in my hibernate.cfg.xmlconfiguration file in the console I can see the SQL.
在控制台的hibernate.cfg.xml配置文件中,我可以看到 SQL。
But it's not realSQL... Can I see the SQL code that will be passed directly to database?
但它不是真正的SQL...我可以看到将直接传递到数据库的SQL代码吗?
Example:
例子:
I see
我懂了
select this_.code from true.employee this_ where this_.code=?
Can I see
我可以看吗
select employee.code from employee where employee.code=12
the realSQL?
在真正的SQL?
采纳答案by Tommaso Taruffi
log4j.properties
log4j.properties
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE
hibernate.cfg.xml
休眠文件.cfg.xml
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
persistence.xml
持久化文件
Some frameworks use persistence.xml
:
一些框架使用persistence.xml
:
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
回答by Pascal Thivent
Can I see (...) the real SQL
我能看到 (...) 真正的 SQL
If you want to see the SQL sent directly to the database (that is formatted similarto your example), you'll have to use some kind of jdbc driver proxy like P6Spy(or log4jdbc).
如果您想查看直接发送到数据库的 SQL(格式类似于您的示例),则必须使用某种 jdbc 驱动程序代理,如P6Spy(或log4jdbc)。
Alternatively you can enable logging of the following categories (using a log4j.properties
file here):
或者,您可以启用以下类别的日志记录(使用log4j.properties
此处的文件):
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
The first is equivalent to hibernate.show_sql=true
, the second prints the bound parameters among other things.
第一个等价于hibernate.show_sql=true
,第二个打印绑定参数等。
Reference
参考
- Hibernate 3.5 Core Documentation
- Hibernate 4.1 Core Documentation
回答by Stephen Denne
select this_.code from true.employee this_ where this_.code=?
iswhat will be sent to your database.
select this_.code from true.employee this_ where this_.code=?
是什么将被发送到您的数据库。
this_
is an alias for that instance of the employee
table.
this_
是该employee
表实例的别名。
回答by Brian Riehman
If you can already see the SQL being printed, that means you have the code below in your hibernate.cfg.xml:
如果您已经可以看到正在打印的 SQL,则意味着您的 hibernate.cfg.xml 中有以下代码:
<property name="show_sql">true</property>
To print the bind parameters as well, add the following to your log4j.properties file:
要打印绑定参数,请将以下内容添加到 log4j.properties 文件中:
log4j.logger.net.sf.hibernate.type=debug
回答by gubby
Worth noting that the code you see is sent to the database as is, the queries are sent separately to prevent SQL injection. AFAIK The ? marks are placeholders that are replaced by the number params by the database, not by hibernate.
值得注意的是,您看到的代码是按原样发送到数据库的,查询是单独发送的,以防止 SQL 注入。AFAIK?标记是由数据库而不是休眠的数字参数替换的占位符。