让hibernate支持递归、start with connect by prior 查询

原创 2012年03月28日 13:11:28

由于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

相关文章推荐

hibernate JPA 单表树形结构 注解配置

public class Category  implements java.io.Serializable {               @Id        @GeneratedValue(st...

JPA实体关系映射

1、       实体关系映射 实体关系是指实体与实体之间的关系,从方向上分为单向关联和双向关联,从实体数量上分为一对一、一对多、多对多等。对于任何两个实体,都要从这两个方面区分它们之间的关系。 单向...

Hibernate注解实现单表递归树形结构

目录: 概述环境代码示例测试结果 [一]、概述 在系统中,经常会用到无限级递归的树形结构,比如菜单、组织机构管理、多级分类等等,一般是在同一个表中定义父子关系实现这种树形结构,本文主要讲述如何运用h...
  • budapest
  • budapest
  • 2013年09月09日 20:48
  • 10936

Java连接常见数据库的连接字符串

1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar Class.forName( "org.gjt.mm.mysql.Driver" ); cn ...

让hibernate支持递归、start with connect by prior 查询

由于hibernate的结果对象映射功能很好用,但是有些特殊的sql文hibernate并不支持,比如start with  connect by prior,如何让hibernate支持这种特殊sq...

让hibernate支持递归、start with connect by prior 查询

让hibernate支持递归、start with connect by prior 查询

Hibernate-HQL基础

Hibernate-HQL基础 基本概述     Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Language)查...

hinernate--推荐使用原生SQL语句进行查询(效率高)

需要注意以下几点: 1 select * from t_stu where name like ? 注意要加上这个addEntity 因为默认返回Obect数组  注意addEntity()里面的...

oracle中用start with...connect by prior子句实现递归查询[例子不错]

今天在做权限这一块,碰到要读取oracle中的树形结构,所以就用到了start with...connect by prior。所以留个脚印以后碰到可以看看。     在oracle中的s...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:让hibernate支持递归、start with connect by prior 查询
举报原因:
原因补充:

(最多只允许输入30个字)