Search code examples
javasqlhibernatecriteriahibernate-criteria

How to get bound parameters out of a Hibernate Criteria object?


I am running a complex query via the Hibernate Criteria API. During debug, I would like to be able to extract and log the parameters that have been bound to the criteria object. Using Hibernate's org.Hibernate.type logger is not an option because during the server start there are many many queries that are run and the logger causes a serious performance hit, and as we are using Hibernate 3.5, it cannot be configured to be turned on before and after the specific method call, only when the server starts.

As far as getting the SQL query itself, in this answer someone posted excellent code that allows for extracting the SQL from a criteria, is there a similar solution for the bound parameters?


Solution

  • You can log the Criteria and the Restrictions will be displayed as well:

    Criteria criteria = session.createCriteria(Post.class)
        .add(Restrictions.eq("title", "post"));
    LOGGER.info("Criteria: {}", criteria);
    

    will display:

    Criteria: CriteriaImpl(com.vladmihalcea.book.hpjp.hibernate.association.AllAssociationTest$Post:this[][title=post])