我已经使用Spring
AOP完成了这样的事情,因此我可以获取应用程序中运行的任何查询的sql,参数,错误和执行时间,无论是HQL,Criteria还是本机SQL。
这显然是脆弱,不安全的,可能会因Hibernate中的更改而中断,等等,但它说明可以获取SQL:
CriteriaImpl c = (CriteriaImpl)query;SessionImpl s = (SessionImpl)c.getSession();SessionFactoryImplementor factory = (SessionFactoryImplementor)s.getSessionFactory();String[] implementors = factory.getImplementors( c.getEntityOrClassName() );CriteriaLoader loader = new CriteriaLoader((OuterJoinLoadable)factory.getEntityPersister(implementors[0]), factory, c, implementors[0], s.getEnabledFilters());Field f = OuterJoinLoader.class.getDeclaredField("sql");f.setAccessible(true);String sql = (String)f.get(loader);尝试将所有物品包装起来并使用,后果自负。



