hibernate 别名的坑:加别名与不加别名解析不一致

hibernate的HQL涉及表连接的查询得使用别名 ,有加别名和美家别名所生成sql是不一样的。

hibernate 别名的坑:加别名与不加别名解析不一致_第1张图片

hibernate 别名的坑:加别名与不加别名解析不一致_第2张图片

加别名解析结果:

Hibernate: select subwaymoni0_.ID as ID, subwaymoni0_.SUBWAY_MONITOR_PROJECT_ID as SUBWAY2_440_, subwaymoni0_.SUBWAY_MONITOR_ITEM_ID as SUBWAY3_440_, subwaymoni0_.CODE as CODE440_, subwaymoni0_.NAME as NAME440_, subwaymoni0_.SUM_CHANGE as SUM6_440_, subwaymoni0_.INITIAL_VALUE as INITIAL7_440_, subwaymoni0_.CHANGE_RATE as CHANGE8_440_, subwaymoni0_.COORDINATE as COORDINATE440_, subwaymoni0_.EMPLOYEE_USERID as EMPLOYEE10_440_ from SUBWAY_MONITOR_POINTS subwaymoni0_ where subwaymoni0_.CODE=? and subwaymoni0_.SUBWAY_MONITOR_ITEM_ID=?

hibernate 别名的坑:加别名与不加别名解析不一致_第3张图片

不加别名解析结果:

Hibernate: select subwaymoni0_.ID as ID, subwaymoni0_.SUBWAY_MONITOR_PROJECT_ID as SUBWAY2_440_, subwaymoni0_.SUBWAY_MONITOR_ITEM_ID as SUBWAY3_440_, subwaymoni0_.CODE as CODE440_, subwaymoni0_.NAME as NAME440_, subwaymoni0_.SUM_CHANGE as SUM6_440_, subwaymoni0_.INITIAL_VALUE as INITIAL7_440_, subwaymoni0_.CHANGE_RATE as CHANGE8_440_, subwaymoni0_.COORDINATE as COORDINATE440_, subwaymoni0_.EMPLOYEE_USERID as EMPLOYEE10_440_ from SUBWAY_MONITOR_POINTS subwaymoni0_ where subwaymoni0_.CODE=? and subwaymoni0_.ID=?

区别在于尾部的subwaymoni0_.SUBWAY_MONITOR_ITEM_ID和 subwaymoni0_.ID不一致

两句解析出来的sql是不一样的,之前那个hibernate解析错误不是缓存问题,hibernate解析规则就是那样。

 

故:

查询尽量加别名吧,以前接触过一些专门搞数据库的人,写sql都习惯加别名
简单查询也都加别名

你可能感兴趣的:(爬坑日记)