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.