【发布时间】:2016-01-21 22:33:18
【问题描述】:
我正在尝试进行 HQL 查询,但我不明白如果我不对 SELECT 语句中的值重新排序,为什么查询会失败
以下查询不起作用
查询 1
@Query("SELECT uf.flight.arrivalAirport.iataCode, uf.flight.flightStatus " +
"FROM UserFlight uf WHERE uf.flight.id=?1 AND uf.user.id=?2")
生成的 SQL
select airport2_.iata_code as col_0_0_,
flight1_.flight_status_id as col_1_0_,
flightstat4_.id as id1_6_,
flightstat4_.code as code2_6_,
flightstat4_.description as descript3_6_
from user_flight userflight0_,
flight flight1_,
airport airport2_
inner join flight_status flightstat4_
on flight1_.flight_status_id=flightstat4_.id
where userflight0_.flight_id=flight1_.id
and flight1_.arrival_airport_id=airport2_.id
and userflight0_.flight_id=?
and userflight0_.user_id=?
例外
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 未知 “on 子句”中的“flight1_.flight_status_id”列
如果我将查询更改为以下内容(只是重新排序 SELECT 值)
查询 2
@Query("SELECT uf.flight.flightStatus, uf.flight.arrivalAirport.iataCode " +
"FROM UserFlight uf WHERE uf.flight.id=?1 AND uf.user.id=?2")
生成的 SQL
select flight1_.flight_status_id as col_0_0_,
airport4_.iata_code as col_1_0_,
flightstat2_.id as id1_6_,
flightstat2_.code as code2_6_,
flightstat2_.description as descript3_6_
from user_flight userflight0_,
flight flight1_
inner join flight_status flightstat2_
on flight1_.flight_status_id=flightstat2_.id,
airport airport4_
where userflight0_.flight_id=flight1_.id
and flight1_.arrival_airport_id=airport4_.id
and userflight0_.flight_id=?
and userflight0_.user_id=?
它有效,我从数据库中获取结果。
谁能告诉我为什么其中一个工作而另一个不工作?
注意:FlightStatus 是一个实体,而不是字符串值。
【问题讨论】:
-
呃。去掉那些下划线。看起来很糟糕
-
@FirebladeDan 这就是 Hibernate 生成的 SQL 的样子。
-
@doelleri - 没有我的味道
-
你使用的是什么版本的休眠?什么版本的 MySQL?
-
Hibernate 版本是 4.3.10.Final