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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4884249/
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 08:27:22  来源:igfitidea点击:

Java /JPA | Query with specified inherited type

javajpaentitymanager

提问by redbull

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 :

我正在一个通用表“Sample”上构建一个查询,我有几种类型从这个表“SampleOne”、“SampleTwo”继承而来。我需要一个查询,如:

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 )

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

I would greatly appreciate any input in this topic,

我将不胜感激对此主题的任何投入,

Kind regards, P.

亲切的问候,P。

回答by axtavt

In JPA 2.0 you can use TYPEexpression (though currently it doesn't work with parameters in Hibernate, see HHH-5282):

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

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

The similar Hibernate-specific expression is .class:

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

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

回答by Sean Patrick Floyd

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

这是Java EE 6 教程相关部分

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。抽象实体就像具体实体,但不能被实例化。

抽象实体可以像具体实体一样被查询。如果抽象实体是查询的目标,则查询对抽象实体的所有具体子类进行操作:

@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 typeis 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.

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

回答by Wim De Rammelaere

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

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

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

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

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

You can workaround this issue by doing something like this:

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

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

回答by Gourav Jindal

Do this in your repository

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

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

Where BaseUserEntityis your parent entity

BaseUserEntity你的父实体在哪里