0

I'm working with hibnernate and I've a piece of code like:

    Criteria criteria = getCurrentSession().createCriteria(objectClass);
    criteria.add(Restrictions.isNull("myFields"));
    System.out.println("SQL: " + criteria.getSQL()); <-- How can I do this?
    return criteria.list();

I'd like to print the SQL (or HQL) to the console, for debugging, but I can't find any method to get this kind of information.

I've already seen many answers on stackoverflow like this and this but those are old questions and I dosen't find any useful answer.

Can anyone help me? Thanks

Community
  • 1
  • 1
Alessandro
  • 4,382
  • 8
  • 36
  • 70

2 Answers2

4

The easiest way to do this -- edit your Hibernate configuration.

You can set up

hibernate.show_sql = true

to enable the logging of all the generated SQL statements to the console.

Moreover you can set up

hibernate.format_sql = true
hibernate.use_sql_comments = true

to make it more readable

Read more about Hibernate configuration here

Kirill Dmitriev
  • 116
  • 1
  • 3
2

if you use persistance.xml then you add property

<property name="hibernate.show_sql" value="true"/>
Piotr Rogowski
  • 3,642
  • 19
  • 24