-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Order by polymorphic hql querys
PostPosted: Wed Jul 06, 2011 6:27 am 
Newbie

Joined: Fri Jan 08, 2010 8:26 am
Posts: 3
Hi,

We're trying to implement an hql query with a polymorphic Entity model like this:

Code:
@Entity
@Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorFormula(value =
   "case " +
   "   when (ACTIVO = 'S' AND TYPE_CUSTOMER = 'A') then 'A' " +
   "   when (ACTIVO = 'S' AND TYPE_CUSTOMER = 'B') then 'B' " +
   "   when (ACTIVO = 'S' AND TIPO = 'C') then 'C' " +
   "end")
@DiscriminatorValue("A")
public class A {

     @Column("nombreA");
     private String nombre;

     public String getNombre()  {
        return nombre;
     }

     public void setNombre(String nombre)  {
        this.nombre = nombre;
     }
}

@Entity
@DiscriminatorValue("B")
public class B extends A {

}

@DiscriminatorValue("C")
public class C extends B {

     @Column("nombreC");
     private String nombre;

     public String getNombre()  {
        return nombre;
     }

     public void setNombre(String nombre)  {
        this.nombre = nombre;
     }
}


We like to perform an hql query like this:

Code:
select b from B b
order by b.nombre


And we want that the b.nombre column on the order by clause will be the "nombre" column if the TYPE(b) = B and "nombreC" if the TYPE(b) = C. Of course, the above example doesn't work, it ever order by the "nombre" column.

We've been testing another sample querys looking for the behaviour we need, but nothing works. We thought something like this query should work:

Code:
select b from B b
order by (case when TYPE(b) = C then b.nombre else b.nombre end)


But in this case the resulting SQL query puts also the "nombre" column at both case options.

We think we need something like a cast operator for polymorphic hql querys to solve that problem (not a SQL cast funcion but an HQL cast).

Somebody could help us?

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.