Sleep mode, how to show query criteria

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 a query executed by a criterion.

I know that I can use the show_sql property and write queries using log4j or any other logging structure, but I need a higher level of logging.

I have such a method

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); } 

Do I need, before fulfilling the .list criterion, to show the query that will be executed?

Is there any utility class in api criteria to show the request as I want?

Thnx in advance

+4
source share
1 answer

I do not know any utility classes in the JPA API. But it is possible to display SQL statements at the hibernate save level.

Add the following line to the persistence.xml file:

  <properties> <property name="hibernate.show_sql" value="true" /> </properties> 

If you use log4j, you can also set the org.hibernate.type logging level to TRACE, which will show you even more detailed information in your logs. Add these lines to your log4j-config (you definitely need to have an adapter called "FILE"):

 <category name="org.hibernate.type"> <priority value="TRACE"/> <appender-ref ref="FILE"/> </category> 

Hope this helps ...

+8
source

Source: https://habr.com/ru/post/1334894/


All Articles