我有这些课程:
@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?
答案 0 :(得分:1)
使用MappedSuperclass
批注
@MappedSuperclass
public abstract class AbstractClazz {