java hibernate: selecting the discriminator column in polymorphic hql query

flybywire picture flybywire · Apr 7, 2011 · Viewed 15.6k times · Source

In hibernate, I want to select the discriminator value. Something like

select discriminator, id, name, age from Animal

The idea is to send the result of this query to the client side, so that I can display a different icon based on the value of the discriminator column (i.e. cat, dog, elephant, etc).

Is that possible? How?

Answer

axtavt picture axtavt · Apr 7, 2011

You can do it as follows:

select a.class, a.id, a.name, a.age from Animal a

From Hibernate Documentation:

The special property class accesses the discriminator value of an instance in the case of polymorphic persistence.