Enable hibernate to display the binding parameter values of SQL statements

Source: Internet
Author: User

The built-in property <property name = "show_ SQL"> true </property> provided by hibernate can only output SQL statements similar to the following:

Hibernate:
Insert into student (name, sex, age, cardid, classroom_id, ID) values (?, ?, ?, ?, ?, ?)

This is not conduciveProgramDebugging, In order to display? Third-party jar packages are required for the specific data represented by placeholders. p6spy is an open-source implementation of this requirement.

1. Use p6spy in a Java project:

    1. Download its jar package to its official website, http://www.p6spy.com/(seemingly inaccessible) by Google or Baidu search it.
    2. Download a ZIP or jar file package, decompress it, put the p6spy. jar file in the build path, and put the spy. properties file into the src directory.
    3. Modify two attributes. The first one is realdriver = the driver class of your specific database (for example, MySQL is: COM. mySQL. JDBC. driver); second, logfile = specifies the location of the log file (for example, C:/spy. log ).
    4. Modify <property name = "hibernate. Connection. driver_class"> com. p6spy. Engine. Spy. p6spydriver </property> In the hibernate. cfg. xml configuration file.

After the configuration is complete, run the hql statement and check the spy. log file? The specific placeholder data: (the previous part is the hibernate initialization information, and the complete SQL statement is shown at the bottom)

1326730516837 |-1 | debug | com. p6spy. Engine. Common. p6spyoptions reloading Properties
1326730516842 |-1 | info | using properties file: D: \ workspace \ myeclipse \ 9 \ hibernate \ list mapping \ bin \ spy. Properties
1326730516842 |-1 | info | no value in Environment for: getstacktrace, using: false
1326730516842 |-1 | info | no value in Environment for: getappender, using: COM. p6spy. Engine. Logging. appender. filelogger
1326730516842 |-1 | info | no value in Environment for: getderegisterdrivers, using: false
1326730516842 |-1 | info | no value in Environment for: getuseprefix, using: false
1326730516842 |-1 | info | no value in Environment for: getexecutionthreshold, using: 0
1326730516842 |-1 | info | no value in Environment for: getautoflush, using: True
1326730516842 |-1 | info | no value in Environment for: getexclude, using:
1326730516842 |-1 | info | no value in Environment for: getexcludecategories, using: info, debug, result, batch
1326730516843 |-1 | info | no value in Environment for: getfilter, using: false
1326730516843 |-1 | info | no value in Environment for: getinclude, using:
1326730516843 |-1 | info | no value in Environment for: getincludecategories, using:
1326730516843 |-1 | info | no value in Environment for: getlogfile, using: spy. Log
1326730516843 |-1 | info | no value in Environment for: getrealdriver, using: COM. MySQL. JDBC. Driver
1326730516843 |-1 | info | no value in Environment for: getrealdriver2, using:
1326730516843 |-1 | info | no value in Environment for: getrealdriver3, using:
1326730516843 |-1 | info | no value in Environment for: getappend, using: True
1326730516843 |-1 | info | no value in Environment for: getspydriver, using: COM. p6spy. Engine. Spy. p6spydriver
1326730516843 |-1 | info | no value in Environment for: getdateformat, using:
1326730516843 |-1 | info | no value in Environment for: getdateformatter, using: NULL
1326730516843 |-1 | info | no value in Environment for: getstringmatcher, using: COM. p6spy. Engine. Common. substringmatcher
1326730516843 |-1 | info | no value in Environment for: getstringmatcherengine, using: COM. p6spy. Engine. Common. substringmatcher @ 60e128
1326730516843 |-1 | info | no value in Environment for: getstacktraceclass, using:
1326730516843 |-1 | info | no value in Environment for: getsqlexpression, using: NULL
1326730516843 |-1 | info | no value in Environment for: getreloadproperties, using: false
1326730516843 |-1 | info | no value in Environment for: getreloadpropertiesinterval, using: 60
1326730516843 |-1 | info | no value in Environment for: getjndicontextfactory, using: NULL
1326730516843 |-1 | info | no value in Environment for: getjndicontextproviderurl, using: NULL
1326730516844 |-1 | info | no value in Environment for: getjndicontextcustom, using: NULL
1326730516844 |-1 | info | no value in Environment for: getrealdatasource, using: NULL
1326730516844 |-1 | info | no value in Environment for: getrealtrans ceclass, using: NULL
1326730516844 |-1 | info | no value in Environment for: getrealdatasourceproperties, using: NULL
1326730517419 | 7 | 0 | statement | select max (ID) from classroom
1326730517440 | 0 | 0 | statement | select max (ID) from student
1326730517454 | 0 | 0 | statement | insert into classroom (grade, number, ID) values (?, ?, ?) | Insert into classroom (grade, number, ID) values ('Fourth grad', 1, 1)
1326730517456 | 1 | 0 | statement | insert into student (name, sex, age, cardid, classroom_id, ID) values (?, ?, ?, ?, ?, ?) | Insert into student (name, sex, age, cardid, classroom_id, ID) values ('lisi', 'true', 21, '123', 1, 2)
1326730517457 | 0 | 0 | statement | update student set classroom_id = ?, Index _ =? Where id =? | Update student set classroom_id = 1, index _ = 1 where id = 2
1326730517458 | 0 | 0 | commit |

2. Use p6spy in Java Web projects (in Tomcat environment)

    1. Put p6spy. jar into the WEB-INF/lib directory of the application, put spy. properties into the WEB-INF/classes directory
    2. Others are used in the same way as Java project projects.
    3. Restart Tomcat server

Possible problems: Solve the Problem of driver Loading Sequence

If spy. the database driver name of your program is a real driver in spy. properties, but it has been loaded before p6spy. p6spy will not wrap these connections. either prevent the driver from loading, or try setting 'deregisterdrivers' to true in spy. properties.
put spy. change deregisterdrivers = false to deregisterdrivers = true in the properties file and run the command again.

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.