對於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 = ?