I think this is a simple question although I do not know how to solve it.
In a spring/Hibernate application I need to show the query that a criteria execute.
I know that I can use show_sql property and log the queries using log4j or any other logging framework but what I need is a higher level of logging.
I have a method like this
public void searchIntegrationClient(IntegrationClientSearchCommand integrationClientSearchCommand,PartialList<IntegrationClient> partialList) {
Session session = getSession();
Criteria pageCriteria=session.createCriteria(IntegrationClient.class);
if(StringUtil.isNotEmpty(integrationClientSearchCommand.getNameCmd())){
pageCriteria.add(Restrictions.like("name", integrationClientSearchCommand.getNameCmd(), MatchMode.START));
}
//adding ordering alphabetically
pageCriteria.addOrder(Order.asc("name"));
pageCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<IntegrationClient> list = (List<IntegrationClient>)pageCriteria.list();
partialList.setPartialResultList(list);
Criteria countCriteria=session.createCriteria(IntegrationClient.class);
if(StringUtil.isNotEmpty(integrationClientSearchCommand.getNameCmd())){
countCriteria.add(Restrictions.like("name", integrationClientSearchCommand.getNameCmd(), MatchMode.START));
}
countCriteria.setProjection(Projections.rowCount());
partialList.setTotalNumberOfRecords(((Integer)countCriteria.uniqueResult()).intValue());
releaseSession(session);
}
I need before executes the criteria.list to show the query that will be executed?
Is there any utility class in the criteria api to show the query like what I want ?
Thnx in advance
The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.
Executing Queries To prepare a query for execution, create a TypedQuery<T> object with the type of the query result by passing the CriteriaQuery object to EntityManager. createQuery . Queries are executed by calling either getSingleResult or getResultList on the TypedQuery<T> object.
Criteria crit = session. createCriteria(Test. class); final ResultTransformer trans = new DistinctRootEntityResultTransformer(); crit. setResultTransformer(trans); List rsList = trans.
I don't know about any utility class in the JPA Criteria API. But there is the possibility of showing the SQL statements on the hibernate persistence level.
Add the following line to your persistence.xml file:
<properties>
<property name="hibernate.show_sql" value="true" />
</properties>
If you are using log4j you can also set the logging level of org.hibernate.type to TRACE, which will show you even more verbose information in your logs. Add these lines to your log4j-config (you need to have some adapter called 'FILE' of course):
<category name="org.hibernate.type">
<priority value="TRACE"/>
<appender-ref ref="FILE"/>
</category>
I hope this helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With