如何解决从另一个实体(JPA)继承的实体的“未指定主键"? [英] How to solve 'no primary key specified' for an Entity that inherits from another Entity (JPA)?

查看:477
本文介绍了如何解决从另一个实体(JPA)继承的实体的“未指定主键"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有所有文档类型共有的超类:

I want to have a superclass that is common to all document types:

@Entity
public abstract class Doc implements Serializable
{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected long docId;

    public long getDocId()
    {
        return docId;
    }

    public void setDocId(long docId)
    {
        this.docId = docId;
    }

}

我想为每种文档类型提供子类:

And I want to have child classes for each doc type:

@Entity
@Table(name = "DocTypeA")
public class DocTypeA extends Doc implements Serializable
{
    // other child fields
}

但是它给出了一个错误,并说DocTypeA需要一个主键.如何隔离主键并将其放在超类中?因为所有子类都将具有相同的id字段.

But it gives an error and says that DocTypeA needs a primary key. How can I isolate the primary key and put it in the super class? Because all the subclasses will have that same id field.

我正在使用EclipseLink.

I am using EclipseLink.

我的另一个问题是:为什么我需要将@Entity放在抽象类中?作为抽象类,它无法实例化,那么将其标记为实体有什么意义呢?真的有必要吗?我不会坚持超类.我只需要隔离所有子类中的通用代码即可.

And my other question is: Why do I need to put @Entity in the abstract class? Being an abstract class it cannot be instantiated, so what is the point of marking it as an Entity? Is it really necessary? I will not persist the superclass. I need it only for isolating the code common in all subclasses.

堆栈跟踪很长,相关部分粘贴如下:

The stack trace is a long one, relevant part is pasted below:

Exception Description: Entity class [class repository.DocTypeA] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.

推荐答案

根据官方JavaDoc 注释@MappedSuperclass:

指定一个类,该类的映射信息适用于从其继承的实体.映射的超类没有为其定义单独的表.

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

这就是您要寻找的.因此,abstract类可以轻松地用于实体的公共属性,在大多数情况下,这些属性是主键或数据库生成的对象标识符.然后,该抽象类的带注释字段将仅映射为具体的子类:

which is what you are looking for. Thus abstract classes can easily be used for common attributes of entities, which in most cases a primary key or DB-generated object identifier is. Annotated fields of that abstract class will then only be mapped for concrete subclasses:

MappedSuperclass注释指定的类可以按照与实体相同的方式进行映射,不同之处在于,由于映射的超类本身不存在任何表,因此映射仅适用于其子类.当应用于子类时,继承的映射将应用于子类表的上下文中.

A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself. When applied to the subclasses the inherited mappings will apply in the context of the subclass tables.

交换抽象类Doc中的@Entity批注,如下所示:

Exchange the @Entity annotation in the abstract class Doc like so:

@MappedSuperclass
public abstract class Doc implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected long docId;
    //...
}

,您应该一切顺利.

希望有帮助.

这篇关于如何解决从另一个实体(JPA)继承的实体的“未指定主键"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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