获得Hibernate Criteria的原生SQL

对于Hibernate QBC查询,有时候我们会遇到需要获得最终SQL的需求,但是Hibernate本身并没有提供该方法。

虽然我们可以使用诸如p6spy之类的第三方组件来输出实际SQL日志,但是那是通过拦截dataSource-SQL实现的。我们仍然无法在程序内部获得最终实际的SQL代码。

那么如何在程序中获得SQL呢。其实有一个方法,那就是模拟一遍Hibernate中Criteria生成SQL的全部过程。

此处提供一个方法,用于通过criteria获取实际SQL:

public static String getCriteriaSql(Criteria criteria) {
	CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;//转型
	SessionImplementor session = criteriaImpl.getSession();//获取SESSION
	SessionFactoryImplementor factory = session.getFactory();//获取FACTORY
	CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl
		.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
	String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());
	CriteriaJoinWalker walker = new CriteriaJoinWalker((OuterJoinLoadable) factory
		.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl
		.getEntityOrClassName(), session.getEnabledFilters());
	return walker.getSQLString();
}


结果如:

/* criteria query */
select this_.id                     as id166_0_,
       this_.arrive_station_code    as arrive2_166_0_,
       this_.arrive_station_name    as arrive3_166_0_,
       this_.clearing_code          as clearing4_166_0_,
       this_.clearing_name          as clearing5_166_0_,
       this_.company_type           as company6_166_0_,
       this_.config_memo            as config7_166_0_,
       this_.confirm_count          as confirm8_166_0_,
       this_.confirm_date           as confirm9_166_0_,
       this_.confirm_quantity       as confirm10_166_0_,
       this_.confirmor              as confirmor166_0_,
       this_.confirmor_name         as confirmor12_166_0_,
       this_.cut_count              as cut13_166_0_,
       this_.cut_qty                as cut14_166_0_,
       this_.cut_tag                as cut15_166_0_,
       this_.delivery_code          as delivery16_166_0_,
       this_.delivery_name          as delivery17_166_0_,
       this_.dept_code              as dept18_166_0_,
       this_.dept_name              as dept19_166_0_,
       this_.dy_reject              as dy20_166_0_,
       this_.dy_reject_code         as dy21_166_0_,
       this_.flow_code              as flow22_166_0_,
       this_.flow_name              as flow23_166_0_,
       this_.going_code             as going24_166_0_,
       this_.going_name             as going25_166_0_,
       this_.is_ocena_shipping      as is26_166_0_,
       this_.oil_code               as oil27_166_0_,
       this_.oil_name               as oil28_166_0_,
       this_.outcontrol_tag         as outcontrol29_166_0_,
       this_.plan_date              as plan30_166_0_,
       this_.plan_station_code      as plan31_166_0_,
       this_.producer               as producer166_0_,
       this_.producer_date          as producer33_166_0_,
       this_.producer_name          as producer34_166_0_,
       this_.quantity               as quantity166_0_,
       this_.railway_name           as railway36_166_0_,
       this_.reject                 as reject166_0_,
       this_.reject_code            as reject38_166_0_,
       this_.remain_count           as remain39_166_0_,
       this_.remain_quantity        as remain40_166_0_,
       this_.remark                 as remark166_0_,
       this_.rerceiv_unit_code      as rerceiv42_166_0_,
       this_.rerceiv_unit_name      as rerceiv43_166_0_,
       this_.return_count           as return44_166_0_,
       this_.return_quantity        as return45_166_0_,
       this_.special                as special166_0_,
       this_.start_station_code     as start47_166_0_,
       this_.start_station_name     as start48_166_0_,
       this_.start_station_type     as start49_166_0_,
       this_.status                 as status166_0_,
       this_.storage_point_code     as storage51_166_0_,
       this_.storage_point_name     as storage52_166_0_,
       this_.suggestion_end_date    as suggestion53_166_0_,
       this_.tank_count             as tank54_166_0_,
       this_.target_code            as target55_166_0_,
       this_.target_name            as target56_166_0_,
       this_.transport_type_code    as transport57_166_0_,
       this_.transport_type_name    as transport58_166_0_,
       this_.uninstall_oiltank_code as uninstall59_166_0_,
       this_.uninstall_oiltank_name as uninstall60_166_0_,
       this_.verifier               as verifier166_0_,
       this_.verifier_name          as verifier62_166_0_,
       this_.verify_date            as verify63_166_0_
  from p_station_multiple this_
 where this_.company_type = ?
   and this_.plan_date = ?
   and this_.target_code = ?



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章