Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate how to show the criteria queries

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

like image 968
Fanooos Avatar asked Jan 10 '11 08:01

Fanooos


People also ask

What are criteria queries in Hibernate?

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.

How do you execute a criteria query?

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.

How can we get unique result in Hibernate criteria?

Criteria crit = session. createCriteria(Test. class); final ResultTransformer trans = new DistinctRootEntityResultTransformer(); crit. setResultTransformer(trans); List rsList = trans.


1 Answers

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...

like image 176
fgysin Avatar answered Sep 22 '22 07:09

fgysin