@Entity
@Table(name = "FOLDER")
public class Folder {
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
privateInteger id;
@Formula("(select case when c.TYPE = 'M' then isnull(c.FIRSTNAME + ' ', '') + c.LASTNAME end" +
" from CONTACT c where c.ID_FOLDER = ID")
privateString masterContact
// getters / setters
}
Using SQLServer2008Dialect, if I want that EJBQL query executed : "select count(distinct folder) from Folder folder", the generated query is :
WITH query AS (select
count(folder0_.id) as col_0_0_,
ROW_NUMBER() OVER (
ORDER BY
CURRENT_TIMESTAMP) as __hibernate_row_nr__
from
folder folder0_
group by
count(folder0_.id)) SELECT
*
FROM
query
WHERE
__hibernate_row_nr__ >= ?
AND __hibernate_row_nr__ < ?
which will throw the SQLServerException #144 "Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause".
I now wanted to execute the following EJBQL query : "select folder from Folder folder order by folder.id" and here is the SQL query generated :
WITH query AS (select
folder0_.id as id1_62_,
(select
case
when c.TYPE = 'M' then isnull(c.FIRSTNAME + ' ', '') + c.LASTNAME
end,
ROW_NUMBER() OVER (
order by
folder0_.id asc) as __hibernate_row_nr__
from
contact c
where
c.id_folder = folder0_.id) as formula0_
from
folder folder0_ ) SELECT
*
FROM
query
WHERE
__hibernate_row_nr__ >= ?
AND __hibernate_row_nr__ < ?
Which will throw the SQLServerException #116 "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
So I implemented myself the "insertRowNumberFunction" by replacing the sql.indexOf( FROM ); with sql.lastIndexOf( FROM );.
Now the SQL generated query was :
WITH query AS (select
folder0_.id as id1_62_,
(select
case
when c.TYPE = 'M' then isnull(c.FIRSTNAME + ' ', '') + c.LASTNAME
end
from
contact c
where
c.id_folder = folder0_.id) as formula0_,
ROW_NUMBER() OVER (
order by
folder0_.id asc) as __hibernate_row_nr__
from
folder folder0_ ) SELECT
*
FROM
query
WHERE
__hibernate_row_nr__ >= ?
AND __hibernate_row_nr__ < ?
But the results are not necessarily ordered by folder.id ...
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira