开发者

hibernate how to show the criteria queries

开发者 https://www.devze.com 2023-02-03 21:53 出处:网络
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 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());

    releaseSes开发者_如何学JAVAsion(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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消