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

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

问题描述

考虑以下层次结构,其中实体 WidgetA WidgetB 扩展了一个抽象 Widget 超类:

  @Entity 
@Inheritance(strategy = InheritanceType.JOINED)
公共抽象类Widget实现Serializable {

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

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


$ b

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

我需要按 serialNumber搜索Widgets ,但我不知道我在运行时搜索的Widget的具体类型。通过 serialNumber 搜索窗口小部件的正确方法是什么,这样如果 serialNumber WidgetA ,那么返回一个 WidgetA 的实例,依此类推?



我试图在 Widget中使用 findyBySerialNumber() DAO,我得到一个错误,告诉我我不能实例化一个抽象类,这很有道理,但我认为持久性提供者会知道如何查看具体的子实体表并返回正确的实例。我可以做到这一点吗?



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

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


解决方案

为您的小部件层次结构明确指定一个鉴别器。我想你可以尝试明确地定义它,因为Spring Data会操作字节码来生成查询,所以我怀疑SpringData需要明确定义这些值。

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


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;
    ...

and

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

and

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

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?

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?

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);
}

解决方案

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天全站免登陆