本文翻译自: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 参考
- Hibernate 3.5 Core Documentation Hibernate 3.5核心文档
- Hibernate 4.1 Core Documentation Hibernate 4.1核心文档
#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. 标记是占位符,由数据库的数字参数替换,而不是由休眠。