如何在 jpql 查询中仅查询超类实体? [英] How do I query for only superclass entities in a jpql query?

查看:23
本文介绍了如何在 jpql 查询中仅查询超类实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="orderType", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="BASE")
@Table(name = "orders")
public class OrderEntity implements Serializable {
...

@Entity
@DiscriminatorValue(value="RECURRING")
public class RecurringOrderEntity extends OrderEntity{
...

我可以使用以下 jpql 找到所有子类 (RecurringOrderEntity):

I can find all the subclasses (RecurringOrderEntity) with the following jpql:

Query q = em.createQuery(
                "SELECT o from RecurringOrderEntity o where "
                + "o.cancellationDate is null "
                + "and o.maxOccurrences = o.occurrence");

用于查找RecurringOrderEntity 实例的实体的JPQL 语法是什么?

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

我使用 Eclipselink 2.0.0 作为 JPA 提供程序.

I am using Eclipselink 2.0.0 as the JPA provider.

谢谢!

推荐答案

用于仅查找不是 RecurringOrderEntity 实例的实体的 JPQL 语法是什么?

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

使用带有 TYPE 运算符的实体类型表达式.像这样的东西(不确定你想要的确切查询,但你明白了):

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

以下是 JPA 2.0 规范的相关部分:

Below, the relevant section of the JPA 2.0 specification:

可以使用实体类型表达式限制查询多态性.这TYPE 运算符返回确切的类型论据.

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 )

entity_type_literal 是由实体名称指定.

使用实体的Java类作为输入参数来指定实体类型.

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

示例:

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

这篇关于如何在 jpql 查询中仅查询超类实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆