简体   繁体   English

Hibernate生成的SQL对MS SQL无效

[英]SQL generated by Hibernate invalid for MS SQL

Hibernate (HQL) generated the following SQL for which I inserted the parameters: Hibernate(HQL)生成了以下我为其插入参数的SQL:

select 
sum(activity0_.calculated_work) as col_0_0_
, employee2_.id as col_1_0_
, projectele1_.id as col_2_0_ 

from 
activity activity0_ 
inner join generic_object activity0_1_ on activity0_.id=activity0_1_.id 
left outer join project_element projectele1_ on     activity0_.project_element_id=projectele1_.id 
left outer join employee employee2_ on activity0_.owner_id=employee2_.id 
left outer join org_unit orgunit3_ on employee2_.org_unit_id=orgunit3_.id 

where 
activity0_1_.deleted=0 and 
activity0_.client_id=22 

 group by 
 employee2_.id order by SUM(activity0_.calculated_work) DESC

Error message: Column 'project_element.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 错误消息:在选择列表中的列'project_element.id'无效,因为它不包含在聚合函数或GROUP BY子句中。

I executed this SQL directly in the SQL Server Studio with the same result. 我直接在SQL Server Studio中执行了此SQL,结果相同。 I commented this line: 我评论了这一行:

, projectele1_.id as col_2_0_ 

The SQL was then accepted by the SQL Server 然后,SQL被SQL Server接受

The table project_element definitely has a column with the name id it is also referenced in the LEFT OUTER JOIN and there this column is not causing an error. 表project_element肯定有一个名为id的列,它在LEFT OUTER JOIN中也被引用,并且此列不会引起错误。

Removing the alias projectele1_ had no effect. 删除别名projectele1_无效。

To me this looks like a really simple SQL statement. 对我来说,这看起来像一个非常简单的SQL语句。 I cannot imagine what is wrong with it and the error message is not helping at all. 我无法想象它出了什么问题,并且错误消息根本没有帮助。 Any ideas what could be wrong with the SQL? 有什么想法SQL可能出什么问题吗?

您的SQL语法错误。如果将projectele1_.id添加到group by子句中,它将起作用。仅聚合函数在具有group by子句的select语句中起作用;或者如果从select中删除projectele1_.id ,则将正常工作。

My mistake. 我的错。 I should have read the error message several times. 我应该多次阅读错误消息。 projectele1_id is not in the group by clause. projectele1_id不在group by子句中。 MS SQL does not allow to include such a column into the select list. MS SQL不允许在选择列表中包括这样的列。 This seems to be consistency check. 这似乎是一致性检查。 Too bad though that the usage of HQL leads to such an exception in SQL Server but not in MySQL Server 太糟糕了,尽管在SQL Server中使用HQL会导致这样的异常,但在MySQL Server中却不会

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM