1

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.

4

2 回答 2

4

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

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

于 2008-12-09T07:01:52.957 回答
3

Hibernate Tools 确实支持这一点:

1) 使用 HQL 编辑器(您可以通过将光标放在查询上并按 Ctrl+1 来自动获取查询,然后有一个在 HQL 编辑器中打开的选项)。

2) 确保您打开/可见动态 SQL 预览视图,然后它将显示 SQL Hibernate 将从您的 HQL 生成。

3)完成;)

于 2009-08-19T08:03:35.627 回答