Get Hibernate's SQL statement We know that Hibernate will resolve HQL into SQL, and maybe we need these SQL at some point. However, there is no public API in the interface of Hibernate, it seems that we have to act. 1. Before you begin 1.1 Know how to read Javadoc API 1.2 Know how to use Ant Compile Hibernate Source Pack 1.3 Hibernate Source Pack in the SRC directory of the Hibernate compressed package, the API document is in the DOC directory 2. When executing the query.list () method, Hibernate outputs SQL in the console. From the API document, we know that the Query interface has an implementation class is net.sf.hibernate.impl.abstractQueryImpl, but this virtual class does not implement the List method, continue to check the subclavant Net.sf.hibernate.impl of the class. QueryImpl, huh, I found it.
Listpublic List list ()
Throws HibernateException
Description Copied from Interface: Query
Return the query results as a
List. If The Query Contains Multiple Results Pre Row, The Results Are Returned in An Instance of
Object [].
Returns:
Result list
Throws:
HibernateException
3. View net.sf.hibernate.impl.QueryImpl source code, find that the implementation is thrown to the session interface, it seems that we have to find the trouble of the session interface.
public List list () throws HibernateException {verifyParameters (); Map namedParams = getNamedParams (); return getSession () find (bindParameterLists (namedParams), getQueryParameters (namedParams));.}
4. The interface is not implemented, so we directly see the source code of the class net.sf.hibernate.Impl.SessionImple
public List find (String query, QueryParameters queryParameters) throws HibernateException {if (log.isTraceEnabled ()) {log.trace ( "find:" query); queryParameters.traceParameters (factory);} queryParameters.validateParameters (); QueryTranslator [ ] q = getQueries (query, false); List results = Collections.EMPTY_LIST; dontFlushFromFind ; // stops flush being called multiple times if this method is recursively called // execute the queries and return all result lists as a single list try {for (INT i = 0; i Getsqlstringpublic string getsqlstring () Description Copied from Class: Loader The Sql Query String to Be Called; Implement by All Subclasses Specified by: Getsqlstring in class Loader 6. But we don't want to handle the queryParameters parameters of the Find method found in step 4, how to do it? Refer to the other Find method private static final Object [] NO_ARGS = ArrayHelper.EMPTY_STRING_ARRAY; private static final Type [] NO_TYPES = ArrayHelper.EMPTY_TYPE_ARRAY; / *** Retrieve a list of persistent objects using a hibernate query * / public List find (String query) throws HibernateException { Return Find (Query, NO_ARGS, NO_TYPES); // he handed over three parameters of interface} public List Find (String Query, Object Value, Type Type) HibernateException {Return Find (Query, New Object [] {Value} , new type [] {type}; // throw another interface handling} public list find (String query, object [] value, type [] types) throws HibernateException {Return Find (Query, New QueryParameters (Types, VALUES))))); // Back to the interface of the 2 parameters. Hands :)} 7. Start modification, copy the code found by step 4 to change the following, then add back to the sessionImpl class public String [] getSQLStrings (String query) throws HibernateException {// the single parameter interface processing method QueryParameters QueryParameters queryParameters = new QueryParameters (NO_ARGS, NO_TYPES); if (log.isTraceEnabled ()) {log.trace ( "find:" query); queryParameters.traceParameters (factory);} queryParameters.validateParameters (); QueryTranslator [] q = getQueries (query, false); String [] results = new String [q.length]; // Create a return value dontFlushFromFind ; // stops flush being called multiplely called f e {{(int i = 0; i Public string [] getSqlstrings (String query) throws hibernateException; 9. After compiling, we can use session.getsqlstrings (query) to get the SQL statement array :)