Java/JPA |指定继承类型的查询

发布于 2024-10-15 08:09:53 字数 269 浏览 3 评论 0原文

我正在通用表“Sample”上构建查询,并且我有几种继承自该表“SampleOne”、“SampleTwo”的类型。 我需要这样的查询:

select s from Sample where s.type = :type

其中 type 是表的鉴别器值。是否可以以任何方式(并避免创建特定于实体的查询,每个 SampleOne、SampleTwo ... 等一个查询)

我将非常感谢此主题中的任何输入,

亲切的问候, P。

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

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,

Kind regards,
P.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方圜几里 2024-10-22 08:09:53

在 JPA 2.0 中,您可以使用 TYPE 表达式(尽管目前它不适用于 Hibernate 中的参数,请参阅 HHH-5282):

select s from Sample s where TYPE(s) = :type

类似的 Hibernate 特定表达式是 .class

select s from Sample s where s.class = :type

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

The similar Hibernate-specific expression is .class:

select s from Sample s where s.class = :type
栀梦 2024-10-22 08:09:53

以下是Java EE 6 教程的相关部分

抽象实体

抽象类
可以通过以下方式声明一个实体
@Entity 修饰类。
抽象的实体就像具体的实体
实体但无法实例化。

抽象实体只需查询
就像具体的实体一样。如果是摘要
实体是查询的目标,
查询对所有具体的操作进行操作
抽象实体的子类:

@Entity
public abstract class Employee {
    @Id
    protected Integer employeeId;
    ...
}
@Entity
public class FullTimeEmployee extends Employee {
    protected Integer salary;
    ...
}
@Entity
public class PartTimeEmployee extends Employee {
    protected Float hourlyWage;
}

如果我没读错,您的查询:

select s from Sample where s.type = :type

如果 type 是鉴别器列,则应该仅返回指定子类型的元素,因此您唯一要做的就是进行强制转换结果列表为您请求的子类型。

Here's the relevant section of the Java EE 6 tutorial:

Abstract Entities

An abstract class
may be declared an entity by
decorating the class with @Entity.
Abstract entities are like concrete
entities but cannot be instantiated.

Abstract entities can be queried just
like concrete entities. If an abstract
entity is the target of a query, the
query operates on all the concrete
subclasses of the abstract entity:

@Entity
public abstract class Employee {
    @Id
    protected Integer employeeId;
    ...
}
@Entity
public class FullTimeEmployee extends Employee {
    protected Integer salary;
    ...
}
@Entity
public class PartTimeEmployee extends Employee {
    protected Float hourlyWage;
}

If I read this right, your query:

select s from Sample where s.type = :type

Should only return elements of the specified subtype if type is the discriminator column, so the only thing that's left for you to do is to cast the result list to your requested sub type.

始于初秋 2024-10-22 08:09:53

在 Hibernate 4.3.7 中你仍然要小心,因为 TYPE() 的实现仍然存在问题,例如:

from SpoForeignPilot sfp where TYPE(sfp.partDocument) = :type

此查询不起作用,因为它错误地检查了 < code>SpoForeignPilot 而不是文档的类型。

您可以通过执行以下操作来解决此问题:

select sfp from SpoForeignPilot sfp join sfp.partDocument doc where TYPE(doc) = :type

You still have to be carefull in Hibernate 4.3.7, because there is still an issue with the implementation of TYPE(), for example:

from SpoForeignPilot sfp where TYPE(sfp.partDocument) = :type

This query doesn't work as it incorrectly checks the type of SpoForeignPilot and not the type of the document.

You can workaround this issue by doing something like this:

select sfp from SpoForeignPilot sfp join sfp.partDocument doc where TYPE(doc) = :type
蹲墙角沉默 2024-10-22 08:09:53

在您的存储库中执行此操作

@Query("SELECT p FROM BaseUserEntity p where p.class=:discriminatorValue")
    public List<BaseUserEntity> findByDiscriminatorValue(@Param("discriminatorValue") String discriminatorValue);

,其中 BaseUserEntity 是您的父实体

Do this in your repository

@Query("SELECT p FROM BaseUserEntity p where p.class=:discriminatorValue")
    public List<BaseUserEntity> findByDiscriminatorValue(@Param("discriminatorValue") String discriminatorValue);

Where BaseUserEntity is your parent entity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文