Hibernate显示真正的SQL [重复]

本文探讨了如何在使用 Hibernate 时查看实际发送到数据库的 SQL 语句,包括使用 P6Spy 或 log4j 属性来显示带有参数值的真实 SQL 代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Hibernate show real SQL [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

if I set 如果我订

<property name="show_sql">true</property>

in my hibernate.cfg.xml configuration file in the console I can see the SQL. 在我的控制台的hibernate.cfg.xml配置文件中,我可以看到SQL。

But it's not real SQL... 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 real SQL? 真正的 SQL?


#1楼

参考:https://stackoom.com/question/Adwb/Hibernate显示真正的SQL-重复


#2楼

Can I see (...) the real SQL 我可以看到(...)真正的SQL

If you want to see the SQL sent directly to the database (that is formatted similar to 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 参考


#3楼

select this_.code from true.employee this_ where this_.code=? is what will be sent to your database. 什么将被发送到您的数据库。

this_ is an alias for that instance of the employee table. this_employee表的该实例的别名。


#4楼

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

#5楼

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 的hibernate.cfg.xml

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

persistence.xml 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"/>

#6楼

Worth noting that the code you see is sent to the database as is, the queries are sent separately to prevent SQL injection. 值得注意的是,您看到的代码按原样发送到数据库,查询将单独发送以防止SQL注入。 AFAIK The ? AFAIK? marks are placeholders that are replaced by the number params by the database, not by hibernate. 标记是占位符,由数据库的数字参数替换,而不是由休眠。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值