java hibernate:在多态hql查询中选择鉴别器列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5584038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:47:02  来源:igfitidea点击:

java hibernate: selecting the discriminator column in polymorphic hql query

javasqldatabasehibernatehql

提问by flybywire

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

在休眠中,我想选择鉴别器值。就像是

select discriminator, id, name, age from Animal

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?

那可能吗?如何?

回答by axtavt

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.

在多态持久化的情况下,特殊属性类访问实例的鉴别器值。

回答by Santiago Taba

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"

Hibernate 查询对象,不知道列。所以除非你的 Animal 对象中有一个名为 discriminator 的属性,否则你不能这样做。您可以在sql中进行查询或获取整个对象然后获取继承的类型,为此您可以使用“instanceof”