Hibernate prints SQL statements and additional parameters

Source: Internet
Author: User

In the Hibernate configuration file hibernate. cfg. xml, there are three settings related to displaying SQL statements. Their values are boolean values:
(1) show_ SQL: whether to display SQL statements
(2) format_ SQL: whether to format the output string to enhance SQL readability
(3) use_ SQL _comments: whether to display comments to indicate what operations have produced this SQL statement.

By default, Hibernate prints SQL statements on the Console. Therefore, after the preceding settings are enabled, you can see the following structured SQL statements on the Console:
Console code:


[Html]/* load collection cc. unmi. test. model. Post. securities */select
02 securities0 _. post_id as post1_7_1 _,
03 security1 _. shareclassid as sharecla1_16_0 _,
04 security1 _. company_id as company2_16_0 _,
05 from
06 Post_Security_Relationship securities0 _
07 inner join
08 unmi. securities security1 _
09 on securities0 _. shareclassid = security1 _. shareclassid
10 where
11 securities0 _. post_id =?

01/* load collection cc. unmi. test. model. Post. securities */select
02 securities0 _. post_id as post1_7_1 _,
03 security1 _. shareclassid as sharecla1_16_0 _,
04 security1 _. company_id as company2_16_0 _,
05 from
06 Post_Security_Relationship securities0 _
07 inner join
08 unmi. securities security1 _
09 on securities0 _. shareclassid = security1 _. shareclassid
10 where
11 securities0 _. post_id =?

 

It can be found that the parameters corresponding to the SQL statement are not visible on the console. In general, Hibernate will work with Log4j, so that hibernate's log file output can be controlled more flexibly. In hibernate, the default output level of parameters corresponding to SQL statements is TRACE, which is lower than the default Log Level DEBUG of Log4j. Therefore, to display parameters, you also need to manually configure log4j and change the output level under hibernate to TRACE:
Log4j.logger.org. hibernate. type. descriptor. SQL. BasicBinder = TRACE
Log4j. loggerorg. hibernate. type. descriptor. SQL. BasicExtractor = TRACE
After this modification, the printed SQL statement will become in the following format:
Console code:
[Html] 01 20:13:40. 710 [http-8080-1] DEBUG org. hibernate. SQL-
02/* load collection cc. unmi. test. model. Post. categories */select
03 categories0 _. post_id as post1_7_1 _,
04 elementite1 _. id as id3_0 _,
05 from
06 Post_Category_Relationship categories0 _
07 inner join
08 unmi. element_item elementite1 _
09 on categories0 _. category_id = elementite1 _. id
10 where
11 categories0 _. post_id =?
12 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicBinder-binding parameter [1] as [INTEGER]-10
13 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicExtractor-found [1002] as column [id3_0 _]
14 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicExtractor-found [10] as column [post1_7_1 _]

01 20:13:40. 710 [http-8080-1] DEBUG org. hibernate. SQL-
02/* load collection cc. unmi. test. model. Post. categories */select
03 categories0 _. post_id as post1_7_1 _,
04 elementite1 _. id as id3_0 _,
05 from
06 Post_Category_Relationship categories0 _
07 inner join
08 unmi. element_item elementite1 _
09 on categories0 _. category_id = elementite1 _. id
10 where
11 categories0 _. post_id =?
12 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicBinder-binding parameter [1] as [INTEGER]-10
13 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicExtractor-found [1002] as column [id3_0 _]
14 20:13:40. 710 [http-8080-1] TRACE org. hibernate. type. descriptor. SQL. BasicExtractor-found [10] as column [post1_7_1 _]


If you want to view the values of the named parameters in the query, you also need to add the following values in the log4j configuration file:
Log4j.logger.org. hibernate. engine. QueryParameters = DEBUG
Log4j.logger.org. hibernate. engine. query. HQLQueryPlan = DEBUG
After the modification, the following result is displayed:
Console code:
[Html]
01 20:13:40. 710 [http-8080-1] org. hibernate. engine. query. HQLQueryPlan-find: from User where email =: email
02 20:13:40. 710 [http-8080-1] org. hibernate. engine. QueryParameters-named parameters: {email = fantasia@sina.com}
03 20:13:40. 726 [http-8080-1] org. hibernate. SQL-
04/* named HQL query findUserByEmail */select
05 user0 _. id as id0 _,
06 user0 _. email as email0 _,
07 user0 _. enabled as enabled0 _,
08 user0 _. encodedPassword as encodedP8_0 _
09 from
10 User user0 _
11 where
12 user0 _. email =?

01 20:13:40. 710 [http-8080-1] org. hibernate. engine. query. HQLQueryPlan-find: from User where email =: email
02 20:13:40. 710 [http-8080-1] org. hibernate. engine. QueryParameters-named parameters: {email = fantasia@sina.com}
03 20:13:40. 726 [http-8080-1] org. hibernate. SQL-
04/* named HQL query findUserByEmail */select
05 user0 _. id as id0 _,
06 user0 _. email as email0 _,
07 user0 _. enabled as enabled0 _,
08 user0 _. encodedPassword as encodedP8_0 _
09 from
10 User user0 _
11 where
12 user0 _. email =? ++ ++

1. In the hibernate configuration file:
Show_ SQL = true
2. Add the following in the log4j. properties file:
Log4j.logger.org. hibernate. SQL = trace
Log4j.logger.org. hibernate. type = trace

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.