views:

2240

answers:

2

Having installed Hibernate Tools in Eclipse, how can I view the would-be generated SQL query of from the JPA query language? (I'm using Hibernate as my JPA implementation)

My Java DAO class looks something like:

public List<Person> findById(int id)
{
    return entityManager.find(Person.class, id);
}
public List<Person> find(String name)
{
    Query q = entityManager.createQuery("SELECT p FROM Person p WHERE name=?");
    q.setParameter(1, name);
    return q.getResultList();
}

I want to see what the corresponding SQL query will be. I've heard that Hibernate Tools has some kind of support for this.

+2  A: 

In order to see SQL query you can just configure hibernate.show_sql=true in your hibernate.cf.xml file. Then you should see the queries in the console window during application execution.

That's the feature of hibernate runtime, when Tools provide you with HQL editor, so you can test the queries before you put them into the code.

Gennady Shumakher
A: 

Hibernate Tools does have support for this:

1) Use the HQL Editor (you can get the query in there automatically by placing the cursor on the query and press Ctrl+1 and then there is an option to Open in HQL Editor).

2) Make sure you have the Dynamic SQL Preview view open/visible then it will show the SQL Hibernate will generate from your HQL.

3) Done ;)

Max Rydahl Andersen