由于hibernate的结果对象映射功能很好用,但是有些特殊的sql文hibernate并不支持,比如start with connect by prior,如何让hibernate支持这种特殊sql查询,还能使用hibernate的对象功能呢。方法如下:
1、普通sql文:select {city.*} from T_IP_CITY_INVEST city start with city.PROFESSION_AUDITOR = ‘wuy’ connect by prior city.PARENT_PROJECT_CODE = city.PROJECT_CODE and order by city.id
2、使用方法
this.getSession().createSQLQuery(sql).addEntity(“city”, TIpCityInvest.class)
.list();即可,从上面可以看出,和普通sql文最大的区别就是 {city.*},这个实际上就是把查询结果集转为映射对象。
3、hibernate解析后的sql文:
Hibernate:
select
city.ID as ID115_0_,
city.PROJECT_CODE as PROJECT2_115_0_,
city.PROJECT_NAME as PROJECT3_115_0_,
city.PARENT_PROJECT_CODE as PARENT4_115_0_,
city.CHILD_COUNT as CHILD5_115_0_,
city.PLAN_YEAR as PLAN6_115_0_,
city.PLAN_VERSION as PLAN7_115_0_,
city.PROFESSION_AUDITOR as PROFESSION8_115_0_,
city.CARRY_OVER_REGION as CARRY9_115_0_,
city.PROJECT_AMOUNT_REGION as PROJECT10_115_0_,
city.PROJECT_YEAR_REGION as PROJECT11_115_0_,
city.PROJECT_YEAR_COUNT_REGION as PROJECT12_115_0_,
city.CARRY_OVER_NANJING as CARRY13_115_0_,
city.PROJECT_AMOUNT_NANJING as PROJECT14_115_0_,
city.PROJECT_YEAR_NANJING as PROJECT15_115_0_,
city.PROJECT_YEAR_COUNT_NANJING as PROJECT16_115_0_,
city.CARRY_OVER_SUZHOU as CARRY17_115_0_,
city.PROJECT_AMOUNT_SUZHOU as PROJECT18_115_0_,
city.PROJECT_YEAR_SUZHOU as PROJECT19_115_0_,
city.PROJECT_YEAR_COUNT_SUZHOU as PROJECT20_115_0_,
city.CARRY_OVER_WUXI as CARRY21_115_0_,
city.PROJECT_AMOUNT_WUXI as PROJECT22_115_0_,
city.PROJECT_YEAR_WUXI as PROJECT23_115_0_,
city.PROJECT_YEAR_COUNT_WUXI as PROJECT24_115_0_,
city.CARRY_OVER_CHANGZHOU as CARRY25_115_0_,
city.PROJECT_AMOUNT_CHANGZHOU as PROJECT26_115_0_,
city.PROJECT_YEAR_CHANGZHOU as PROJECT27_115_0_,
city.PROJECT_YEAR_COUNT_CHANGZHOU as PROJECT28_115_0_,
city.CARRY_OVER_NANTONG as CARRY29_115_0_,
city.PROJECT_AMOUNT_NANTONG as PROJECT30_115_0_,
city.PROJECT_YEAR_NANTONG as PROJECT31_115_0_,
city.PROJECT_YEAR_COUNT_NANTONG as PROJECT32_115_0_,
city.CARRY_OVER_ZHENJIANG as CARRY33_115_0_,
city.PROJECT_AMOUNT_ZHENJIANG as PROJECT34_115_0_,
city.PROJECT_YEAR_ZHENJIANG as PROJECT35_115_0_,
city.PROJECT_YEAR_COUNT_ZHENJIANG as PROJECT36_115_0_,
city.CARRY_OVER_YANGZHOU as CARRY37_115_0_,
city.PROJECT_AMOUNT_YANGZHOU as PROJECT38_115_0_,
city.PROJECT_YEAR_YANGZHOU as PROJECT39_115_0_,
city.PROJECT_YEAR_COUNT_YANGZHOU as PROJECT40_115_0_,
city.CARRY_OVER_TAIZHOU as CARRY41_115_0_,
city.PROJECT_AMOUNT_TAIZHOU as PROJECT42_115_0_,
city.PROJECT_YEAR_TAIZHOU as PROJECT43_115_0_,
city.PROJECT_YEAR_COUNT_TAIZHOU as PROJECT44_115_0_,
city.CARRY_OVER_XUZHOU as CARRY45_115_0_,
city.PROJECT_AMOUNT_XUZHOU as PROJECT46_115_0_,
city.PROJECT_YEAR_XUZHOU as PROJECT47_115_0_,
city.PROJECT_YEAR_COUNT_XUZHOU as PROJECT48_115_0_,
city.CARRY_OVER_YANCHENG as CARRY49_115_0_,
city.PROJECT_AMOUNT_YANCHENG as PROJECT50_115_0_,
city.PROJECT_YEAR_YANCHENG as PROJECT51_115_0_,
city.PROJECT_YEAR_COUNT_YANCHENG as PROJECT52_115_0_,
city.CARRY_OVER_HUAIAN as CARRY53_115_0_,
city.PROJECT_AMOUNT_HUAIAN as PROJECT54_115_0_,
city.PROJECT_YEAR_HUAIAN as PROJECT55_115_0_,
city.PROJECT_YEAR_COUNT_HUAIAN as PROJECT56_115_0_,
city.CARRY_OVER_LIANYUNGANG as CARRY57_115_0_,
city.PROJECT_AMOUNT_LIANYUNGANG as PROJECT58_115_0_,
city.PROJECT_YEAR_LIANYUNGANG as PROJECT59_115_0_,
city.PROJECT_YEAR_COUNT_LIANYUNGANG as PROJECT60_115_0_,
city.CARRY_OVER_SUQIAN as CARRY61_115_0_,
city.PROJECT_AMOUNT_SUQIAN as PROJECT62_115_0_,
city.PROJECT_YEAR_SUQIAN as PROJECT63_115_0_,
city.PROJECT_YEAR_COUNT_SUQIAN as PROJECT64_115_0_,
city.BATCHNUMBER as BATCHNU65_115_0_,
city.PROFESSION_AUDITOR_NAME as PROFESSION66_115_0_
from
T_IP_CITY_INVEST city start with city.PROFESSION_AUDITOR = ‘wuy’
connect by prior city.PARENT_PROJECT_CODE = city.PROJECT_CODE
order by
city.id