如何在 Eclipse 中使用 Hibernate Tools 查看 SQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/351665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to see SQL query using Hibernate Tools in Eclipse?
提问by Steve Kuo
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)
在 Eclipse 中安装了 Hibernate Tools 后,如何查看来自 JPA 查询语言的可能生成的 SQL 查询?(我使用 Hibernate 作为我的 JPA 实现)
My Java DAO class looks something like:
我的 Java DAO 类看起来像:
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.
我想看看相应的 SQL 查询是什么。我听说 Hibernate Tools 对此有某种支持。
回答by Gennady Shumakher
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.
为了查看 SQL 查询,您只需在 hibernate.cfg.xml 文件中配置 hibernate.show_sql=true 即可。然后您应该在应用程序执行期间在控制台窗口中看到查询。
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.
这就是 hibernate 运行时的特性,当 Tools 为您提供 HQL 编辑器时,您可以在将查询放入代码之前对其进行测试。
回答by Max Rydahl Andersen
Hibernate Tools does have support for this:
Hibernate Tools 确实支持此功能:
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).
1) 使用 HQL 编辑器(您可以通过将光标放在查询上并按 Ctrl+1,然后有一个在 HQL 编辑器中打开的选项来自动获取查询)。
2) Make sure you have the Dynamic SQL Preview view open/visible then it will show the SQL Hibernate will generate from your HQL.
2) 确保您已打开/可见动态 SQL 预览视图,然后它将显示将从您的 HQL 生成的 SQL Hibernate。
3) Done ;)
3) 完成;)