首页 > 解决方案 > 覆盖基类中的非标识符属性并使其成为子类中的标识符

问题描述

我有问题。尝试从父类继承未定义为标识符的属性时,但在我的子类中是,出现以下错误:

MappingException:您不能覆盖 [com.test.BaseEntity] 基类或 @MappedSuperclass 中的 [testDate] 非标识符属性,并使其成为 [com.test.ChildEntity] 子类中的标识符!

我的父类:

import java.sql.Timestamp;
import java.time.Clock;
import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;

@MappedSuperclass
public abstract class BaseEntity {

    @Column(name = "CREATION_DATE_UTC")
    protected Timestamp testDate;

    public Timestamp getTestDate() {
        return testDate;
    }

    public void setTestDate(Timestamp testDate) {
        this.testDate = testDate;
    }

    @PrePersist
    public void prePersist() {
        testDate = Timestamp.from(Instant.now(Clock.systemUTC()));
    }
}

我的孩子班:

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;

@Entity(name = "CHILD")
@IdClass(ChildEntityPk.class)
public class ChildEntity extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 4392848588125748902L;

    @Id
    @Column(name = "CHILD_ID", insertable = false, updatable = false)
    @JsonIgnore
    private Integer id;

    @Id
    @Column(name = "CHILD_TEST_DATE", insertable = false, updatable = false)
    @JsonIgnore
    private Timestamp testDate;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Timestamp getTestDate() {
        return testDate;
    }

    public void setTestDate(Timestamp testDate) {
        this.testDate = testDate;
    }


    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}

标签: javajpa

解决方案


推荐阅读