Java / JPA |使用指定的继承类型进行查询 [英] Java /JPA | Query with specified inherited type
问题描述
我正在构建一个通用表Sample的查询,我有几种类型继承自这个表SampleOne,SampleTwo。
我需要一个类似的查询:
I am building a query on a generic table "Sample" and I have several types which inherit from this table "SampleOne", "SampleTwo". I require a query like :
select s from Sample where s.type = :type
其中type将是表的鉴别值。是否有可能(并避免创建特定于实体的查询,每个SampleOne,SampleTwo等等)
where type would be a discriminator value of the table. Is it possible in any way ( and avoid to create an entity specific queries, one for each SampleOne, SampleTwo... etc )
我将非常感谢此处的任何输入话题,
I would greatly appreciate any input in this topic,
亲切的问候,
P。
Kind regards, P.
推荐答案
在JPA 2.0中,您可以使用 TYPE
表达式(尽管目前它不适用于Hibernate中的参数,请参阅 HHH-5282 ):
In JPA 2.0 you can use TYPE
expression (though currently it doesn't work with parameters in Hibernate, see HHH-5282):
select s from Sample s where TYPE(s) = :type
类似Hibernate的特定表达式是 .class
:
The similar Hibernate-specific expression is .class
:
select s from Sample s where s.class = :type
这篇关于Java / JPA |使用指定的继承类型进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!