Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java hibernate: selecting the discriminator column in polymorphic hql query

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?

like image 704
flybywire Avatar asked Apr 07 '11 16:04

flybywire


2 Answers

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.

like image 59
axtavt Avatar answered Sep 24 '22 07:09

axtavt


Hibernate query objects, does not know columns. So unless you have a property named discriminator in your Animal object you cant do that. You can do the query in sql or get the entire object and then to get the inherited type, for that you can use "instanceof"

like image 26
Santiago Taba Avatar answered Sep 26 '22 07:09

Santiago Taba