Issue Type: Bug Bug
Affects Versions: 4.0.1
Assignee: Unassigned
Components: core
Created: 04/Nov/12 4:49 PM
Description:

Here is my entity :

@Entity
@Table(name = "FOLDER")
public class Folder {
  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;

  @Formula("(select case when c.TYPE = 'M' then isnull(c.FIRSTNAME + ' ', '') + c.LASTNAME end" +
    " from CONTACT c where c.ID_FOLDER = ID")
  private String 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".

So I applied the patched proposed by HHH-6627.

It then generates the good count method.

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 ...

I'm on SQLServer 2008 R2.

Project: Hibernate ORM
Priority: Blocker Blocker
Reporter: Anthony Ogier
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