从Hibernate 3迁移到4后,SQL别名不再起作用

时间:2014-07-15 11:28:22

标签: java sql hibernate hibernate-mapping

我正在将Struts2 Web应用程序从Hibernate V3迁移到Hibernate 4.3.5。

在Hibernate V3中,我们使用如下的SQL语句,它们运行良好

select u.id as id, 
       u.userId as userId, 
       sum(u.totalSearchedFields) as total, 
       u.date as date, 
       u.status as status 
from user u;

迁移到Hibernate V4.3.5后,上面的SQL语句显示错误:

  

org.hibernate.QueryException:, expected in SELECT

我们通过从SQL语句中删除别名来实现它,如下所示:

select u.id, 
       u.userId, 
       sum(u.totalSearchedFields), 
       u.date, 
       u.status 
from user u;

有没有人认识到这个原因?如何在不删除别名的情况下解决此问题?

1 个答案:

答案 0 :(得分:1)

显然,根据Hibernate 4 developer guide,别名应该用于"反向"以前的方式:

SELECT <PROPERTY_NAME> AS <ALIAS_TABLE.PROPERTY_NAME> 
  FROM TABLE AS <ALIAS_TABLE>

所以尝试改变这个:

select u.id as id, 
       u.userId as userId, 
       sum(u.totalSearchedFields) as total, 
       u.date as date, 
       u.status as status 
from user u;

到此:

select id as {u.id},
       userId as {u.userId}, 
       sum(totalSearchedFields) as total, 
       date as {u.date}, 
       status as {u.status}
from user u;

这种方法似乎与SQL的方法略有不同,因为这些别名旨在被Hibernate用来防止多个具有相同名称的列发生冲突,而在SQL中它们用于生成用户 - 友好的ResultSet。

远射BTW,希望有所帮助。