How do I query for only superclass entities in a jpql query?

强颜欢笑 提交于 2019-11-28 12:12:10

What is the JPQL syntax for finding only entities that are not instances of RecurringOrderEntity?

Use an entity type expression with the TYPE operator. Something like this (not sure about the exact query you want but you get the idea):

SELECT o 
FROM OrderEntity o 
WHERE TYPE(o) <> RecurringOrderEntity
  AND o.cancellationDate is null
  AND o.maxOccurrences = o.occurrence

Below, the relevant section of the JPA 2.0 specification:

4.6.17.4 Entity Type Expressions

An entity type expression can be used to restrict query polymorphism. The TYPE operator returns the exact type of the argument.

The syntax of an entity type expression is as follows:

entity_type_expression ::=
       type_discriminator |
       entity_type_literal |
       input_parameter
type_discriminator ::=
       TYPE(identification_variable |
            single_valued_object_path_expression |
            input_parameter )

An entity_type_literal is designated by the entity name.

The Java class of the entity is used as an input parameter to specify the entity type.

Examples:

SELECT e
FROM Employee e
WHERE TYPE(e) IN (Exempt, Contractor)

SELECT e
FROM Employee e
WHERE TYPE(e) IN (:empType1, :empType2)

SELECT e
FROM Employee e
WHERE TYPE(e) IN :empTypes

SELECT TYPE(e)
FROM Employee e
WHERE TYPE(e) <> Exempt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!