简体   繁体   English

没有ID的JPA / Hibernate继承

[英]JPA/Hibernate inheritance without id

Is it possible to make inheritance in JPA/Hibernate without id? 是否可以在没有id的JPA / Hibernate中进行继承?
My use case: 我的用例:

I have multiple entities and I want every time change is being done, timestamp being recorded. 我有多个实体,我想每次进行更改时都要记录时间戳。 So I created AbstractResource and want each derived class inherit properties and logic (to avoid writing same stuff over and over again in each class). 因此,我创建了AbstractResource并希望每个派生类都继承属性和逻辑(以避免在每个类中重复编写相同的内容)。

My problem that hibernate wants an ID to entity, and I do not care about id, since my only concern is additional properties. 我的问题是休眠状态需要一个ID到实体,我不在乎id,因为我唯一关心的是附加属性。 And each entity can have whatever id it wants (String, int, long, different name, etc.). 每个实体都可以具有所需的任何ID(字符串,整数,长整数,不同的名称等)。

I tried with Embeddable, but looks like hibernate does not support inheritance for Embeddable. 我尝试使用Embeddable,但是看起来冬眠不支持Embeddable的继承。 Do you have any ideas, how my task can be achieved? 您有什么想法可以实现我的任务吗?

My parent class from which "Audited" entities are derived: 我的父类,从中派生“经审核”的实体:

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

@MappedSuperclass is an annotation for super classes that you can extend and use in audit. @MappedSuperclass是可在审核中扩展和使用的超类的注释。 Please see example 请看例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM