如何将属性的值传播到类中其他类型的属性?

时间:2018-07-05 12:50:13

标签: java hibernate jpa

我有这些课程:

@MappedSuperclass
public abstract class AbstractClazz {

    @Id
    protected int id;
    protected Instant createdAt;
    protected String createdBy;

    //getters and setters

    @PrePersist
    private void prePersist() {
        setCreatedAt(Instant.now());
    }

}

public class ConcreteClazzA extends AbstractClazz {

    private String name;

    //getters and setter
}

public class ConcreteClazzB extends AbstractClazz {

    private ConcreteClazzA clazzA;

    private String name;

    //getters and setter
}

当我保留ConcreteClazzA时,如何在ConcreteClazzB上设置createdBy(获取当前用户)?我不能使用@PrePersist,因为我必须保持组件的独立性。

如何在两个类中设置相同的createdAt?

1 个答案:

答案 0 :(得分:1)

使用MappedSuperclass批注

@MappedSuperclass
public abstract class AbstractClazz {