我在JPA中的@Id有问题,如下所述? 有一个通用类如下?
@MappedSuperclass
public abstract class BaseEntity<T> implements Serializable {
private static final long serialVersionUID = 4295229462159851306L;
private T id;
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
}
还有另一个课程如下所示?
@Entity
@Table(name = "DOC_CHANGE_CODE" )
public class ChangeCode extends BaseEntity<Long> {
@Id
@GeneratedValue(generator = "sequence_db", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "sequence_db", sequenceName = "SEQ_DOC_CHANGE_CODE", allocationSize = 1)
public Long getId () {
return super.getId();
}
}
因为任何子类都有自己的序列,所以我必须指定任何子类@Id,因为我覆盖了它的getter并在其中添加了一些注释。不幸的是它无法正常工作。 我如何解决问题并实现目标?
答案 0 :(得分:0)
试试这个:
@Entity
@Table(name = "DOC_CHANGE_CODE" )
@SequenceGenerator(name = "sequence_db", sequenceName = "SEQ_DOC_CHANGE_CODE", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "ID"))
public class ChangeCode extends BaseEntity<Long> {
@Override
@Id
@GeneratedValue(generator = "sequence_db", strategy = GenerationType.SEQUENCE)
public Long getId() {
return id;
}
}
答案 1 :(得分:0)
无法从基类覆盖@Id
。
唯一的方法是提供your own custom IentifierGenerator
并根据子类提供不同的逻辑(例如,使用基于子类名称的序列名称)。