从hibernate获取SQL [英] get SQL from hibernate get

查看:205
本文介绍了从hibernate获取SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 session.get(类名,id)调用中获取SQL查询?我之前看到过这篇文章:如何获取SQL来自Hibernate Criteria API(* not * for logging),但它不包括获取查询。



澄清:我正在寻找一种API方法来获取查询字符串,而不是来自查询日志。换句话说,我问是否有一个方法(如下面的 toSql )而不是:

  Foo foo = session.get(Foo.class,id); 

我可以这样说:

  String sqlOrHql = session.get(Foo.class,id).toSql(); 
//执行sqlOrHql以通过hibernate或手动使用JDBC来获取foo


解决在 persistence.xml 中启用Hibernate Statistics
$ b $

 <属性名称= hibernate.generate_statistics >真< /性> 

然后使用 SessionFactory.getStatistics()和访问必要的statiscits(可能 getQueries())。



更多信息请参阅Hibernate Statistics docs 这里此处这篇文章在上。



编辑 p>

如果您想在不记录日志的情况下执行此操作,则必须使用JDBC驱动程序代理,例如 log4jdbc p6spy jd BC-跟踪包装

Is there a way to get the SQL query from a session.get(class name, id) call? I saw this post earlier: How to get SQL from Hibernate Criteria API (*not* for logging) but it does not cover get queries.

Clarification: I am looking for an API method to get the query string, not from the query log. In other words I am asking if there is a method (like toSql below) where instead of:

Foo foo = session.get(Foo.class, id);

I can say something like:

String sqlOrHql = session.get(Foo.class, id).toSql();
// execute sqlOrHql to get foo, either through hibernate or manually using JDBC

解决方案

Enable Hibernate Statistics in persistence.xml

<property name="hibernate.generate_statistics">true</property>

Then use SessionFactory.getStatistics() and access necessary statiscits (probably getQueries()).

For more info see Hibernate Statistics docs here or here or this post on SO.

EDIT:

If you want to do it without logging, you'll have to use JDBC driver proxy, for example log4jdbc, p6spy or jdbc-trace-wrapper.

这篇关于从hibernate获取SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆