Karthikeyan
Karthikeyan

Reputation: 87

SQL aliases not working anymore after migrating from Hibernate 3 to 4

I am migrating a Struts2 Web Application from Hibernate V3 to Hibernate 4.3.5.

In Hibernate V3 we used SQL statements like the following, and they worked well :

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;

After migrating to Hibernate V4.3.5, the above SQL statement shows the error:

org.hibernate.QueryException: , expected in SELECT

We made it work by removing the aliases from the SQL statement, like this:

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

Does anyone recognize the cause for this? How to fix this problem without removing the aliases?

Upvotes: 2

Views: 1406

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

Appearently, according to the Hibernate 4 developer guide, the aliases should be used in an "inversed" way from what you did before:

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

So try changing this:

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;

to this:

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

The approach seems a bit different from the SQL's one, because this aliases are intended to be used by Hibernate to prevent multiple columns with the same name to conflict, while in SQL they were used to generate an user-friendly ResultSet.

A long shot BTW, hope that helps.

Upvotes: 1

Related Questions