• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

获得hibernate的sql语句(2.1.6)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 118次 | 进入软件测试论坛讨论

领测软件测试网
获得hibernate的sql语句我们知道hibernate会将hql解析成sql,也许在某些时候,我们需要这些sql。不过hibernate的接口中没有公开的api,看来我们得自己行动了。1.开始前的工作  1.1 知道如何阅读javadoc api  1.2 知道如何使用ant编译hibernate源码包  1.3 hibernate源码包在hibernate压缩包的src目录,api文档在doc目录2.在执行Query.list()方法时,hibernate在控制台输出sql。从api文档中我们知道, Query接口有个实现类.net.sf.hibernate.impl.AbstractQueryImpl,但这个虚类中并没有实现list方法,继续查该类的子类net.sf.hibernate.impl.QueryImpl,呵呵,找到了。 list
public List list() throws HibernateException
Description copied from interface: Query Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

Returns: the result list Throws: HibernateException3.查看net.sf.hibernate.impl.QueryImpl源代码,发现这里吧实现丢给了Session接口,看来我们得去找Session接口的麻烦。 public List list() throws HibernateException {    verifyParameters();    Map namedParams = getNamedParams();    return getSession().find( bindParameterLists(namedParams), getQueryParameters(namedParams) );}4.接口是没有实现代码的,因此我们直接看实现类net.sf.hibernate.impl.SessionImple的源代码 public List find(String query, QueryParameters queryParameters) throws HibernateException {
    if ( log.isTraceEnabled() ) {        log.trace( "find: " + query );        queryParameters.traceParameters(factory);    }

    queryParameters.validateParameters();
    QueryTranslator[] q = getQueries(query, false);
    List results = Collections.EMPTY_LIST;

    dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called

    //execute the queries and return all result lists as a single list    try {        for ( int i=0; i<q.length; i++ ) {            List currentResults;            try {                currentResults = q[i].list(this, queryParameters); // 原来list是由这个类做的            }            catch (SQLException sqle) {                throw new JDBCException("Could not execute query", sqle);            }            currentResults.addAll(results);            results = currentResults;        }    }    finally {        dontFlushFromFind--;    }    return results;}
5. 在api文档左下角All Classes框架搜索QueryTranslator,查到后点击查看api,嗯,有料。 getSQLString
public String getSQLString()
Description copied from class: Loader The SQL query string to be called; implemented by all subclasses

Specified by: getSQLString in class Loader6.但是我们并不想处理第4步中找到的find方法的QueryParameters参数,怎么办捏?参考一下其他find方法 private static final Object[] NO_ARGS = ArrayHelper.EMPTY_STRING_ARRAY;private static final Type[] NO_TYPES = ArrayHelper.EMPTY_TYPE_ARRAY;
/*** Retrieve a list of persistent objects using a hibernate query*/public List find(String query) throws HibernateException {    return find(query, NO_ARGS, NO_TYPES); // 他交给了三个参数的接口 }

public List find(String query, Object value, Type type) throws HibernateException {    return find( query, new Object[] { value }, new Type[] { type } ); // 又丢给另一个接口处理 }

public List find(String query, Object[] values, Type[] types) throws HibernateException {    return find(query, new QueryParameters(types, values) ); // 回到2参数的接口手里了 :) }

 
7.开始修改,将第4步找到的代码复制出来更改如下,然后添加回SessionImpl类 public String[] getSQLStrings(String query) throws HibernateException {    // 采用单参数接口处理queryParameters的方法     QueryParameters queryParameters = new QueryParameters(NO_ARGS, NO_TYPES);    if ( log.isTraceEnabled() ) {        log.trace( "find: " + query );        queryParameters.traceParameters(factory);    }
    queryParameters.validateParameters();

    QueryTranslator[] q = getQueries(query, false);
    String[] results = new String[q.length]; // 创建返回值
    dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called

    //execute the queries and return all result lists as a single list    try {        for ( int i=0; i<q.length; i++ ) {            try {                results[i] = q[i].getSQLString(); // 获得sql语句 :)             }            catch (SQLException sqle) {                throw new JDBCException("Could not execute query", sqle);            }        }    }    finally {        dontFlushFromFind--;    }    return results;}
8.给Session接口增加一个方法 public String[] getSQLStrings(String query) throws HibernateException;9.编译之后大功告成,我们可以使用Session.getSQLStrings(query)获得sql语句数组 :)

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网