【问题标题】:JPA/Hibernate inheritance without id没有 id 的 JPA/Hibernate 继承
【发布时间】:2015-07-24 03:53:36
【问题描述】:

是否可以在没有 id 的情况下在 JPA/Hibernate 中进行继承?
我的用例:

我有多个实体,我希望每次进行更改时都记录时间戳。所以我创建了 AbstractResource 并希望每个派生类都继承属性和逻辑(以避免在每个类中一遍又一遍地编写相同的东西)。

我的问题是hibernate想要一个实体的ID,我不关心id,因为我唯一关心的是附加属性。每个实体都可以有任何它想要的 id(String、int、long、不同的名称等)。

我尝试使用 Embeddable,但似乎休眠不支持 Embeddable 的继承。你有什么想法,我的任务如何完成?

派生“已审核”实体的父类:

@Embeddable
@EntityListeners(AbstractResourceListener.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class AbstractResource {
    private long modifyTimestamp;

    @Column(name = "_MODIFY_TIMESTAMP", nullable = true)
    public long getModifyTimestamp() {
    return modifyTimestamp;
    }

    public void setModifyTimestamp(long modifyTimestamp) {
    this.modifyTimestamp = modifyTimestamp;
    }
}

【问题讨论】:

    标签: java hibernate jpa inheritance


    【解决方案1】:

    @MappedSuperclass 是超类的注解,您可以在审计中扩展和使用它。请看example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多