Spring Data JPA(Hibernate):如何仅使用抽象超类中的字段检索具体实体? [英] Spring Data JPA(Hibernate): How do I retrieve a concrete entity using only a field in its abstract superclass?

查看:25
本文介绍了Spring Data JPA(Hibernate):如何仅使用抽象超类中的字段检索具体实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下层次结构,其中实体 WidgetAWidgetB 扩展了抽象的 Widget 超类:

Consider the following hierarchy, where entities WidgetA and WidgetB extend an abstract Widget superclass:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Widget implements Serializable  {

    @Column(name="serialNumber", length=64, nullable=false, unique=true)
    private String serialNumber;
    ...

@Entity
public class WidgetA extends Widget implements Serializable  {
...

@Entity
public class WidgetB extends Widget implements Serializable  {
...

我需要通过 serialNumber 搜索 Widget,但我不知道我在运行时搜索的 Widget 的具体类型.通过 serialNumber 搜索小部件的正确方法是什么,如果 serialNumberWidgetA 的那个,那么 的一个实例WidgetA 被返回,等等?

I need to search for Widgets by serialNumber, but I don't know the concrete type of the Widget I'm searching for at runtime. What is the correct way to search for widgets by serialNumber such that if the serialNumber is that of a WidgetA, then an instance of WidgetA gets returned, and so on?

我正在尝试在 Widget DAO 中使用 findyBySerialNumber(),但我收到一个错误,告诉我我无法实例化一个抽象类,它有道理,但我认为持久性提供者会知道如何查看具体的子实体表并返回正确的实例.我可以让它这样做吗?

I am trying to use a findyBySerialNumber() in the Widget DAO, and I'm getting an error telling me I can't instantiate an abstract class, which makes sense, but I thought the persistence provider would know how to look in the concrete child entity tables and return the correct instance. Can I make it do this?

我使用的是Spring Data JPA",所以 Widget DAO 非常简单:

I am using "Spring Data JPA", so the Widget DAO is really simple:

public interface WidgetDAO extends JpaRepository<Widget, Long> {
    public Widget findBySerialNumber(String serialNumber);
}

推荐答案

您似乎没有为小部件层次结构明确指定鉴别器.我认为您可以尝试明确定义它,因为 Spring Data 将操纵字节码来生成查询,因此我怀疑 SpringData 需要明确定义这些值.

It seems you didn't specify a discriminator explicitly for your widget hierarchy. I think you can try to define it explicitly because Spring Data will manipulate bytecode to generate the queries, and so I suspect SpringData need to have those values explicitely defined.

此外,在子类中,您需要指明每个子类的鉴别器值.

Additionally in subclasses you need to indicate the discriminator value for each subclass.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="WIDGET_TYPE")
public abstract class Widget implements Serializable  {

    @Column(name="serialNumber", length=64, nullable=false, unique=true)
    private String serialNumber;
    ...

-

@Entity
@DiscriminatorValue("A")
public class WidgetA extends Widget implements Serializable  {
...

-

@Entity
@DiscriminatorValue("B")
public class WidgetB extends Widget implements Serializable  {
...

这篇关于Spring Data JPA(Hibernate):如何仅使用抽象超类中的字段检索具体实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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