1

I am using Hibernate tools 3.3 in Eclipse Indigo.

Is there any way to view the Sql equivalent query for the criteria that I created?

There is one Hibernate Dynamic SQL View which shows Sql preview for Hql editor. But I haven't find any preview for criteria.

Dinoop paloli
  • 633
  • 2
  • 8
  • 25

1 Answers1

0

With Hibernate Criteria API the only way to view the SQL output is to run the query. No preview. In order to view the generated SQL you must configure your datasource to log your sql statements. Here is a persistence.xml example for Hibernate MS SQLServer dialect. The ="true" elements are all instructions to be verbose when running Hibernate queries. This has a big impact on performance, needless to say.

    <persistence-unit name="projectPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/projectDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.use_sql_comments" value="true" />
        </properties>
    </persistence-unit>

The sql will only be generated when you run criteria.list()

   Criteria crit = session.createCriteria(Foo.class);
   // create aliases and projections etc. whose effects are not visible yet

   List<Foo> fooList = crit.list(); // only now can you see errors!

See Logging hibernate SQL using log4j

Community
  • 1
  • 1
carbontax
  • 2,164
  • 23
  • 37
  • How to log sql statements? How can I configure that. If you have any tutorial link please share to me. – Dinoop paloli May 07 '13 at 06:52
  • Do you have a logging framework? I just Googled "log sql hibernate log4j" and got a tutorial but I don't know what you are using. – carbontax May 07 '13 at 10:57