【发布时间】:2013-11-14 15:39:33
【问题描述】:
在将@Id分配给父实体中的某个字段后,是否有一种方法可以重新分配子实体中的@Id
例如:
@MappedSuperclass
@Access(AccessType.FIELD)
public abstract class Parent implements Serializable {
@Id
protected Integer parentId;
public Integer getId() {
return parentId;
}
public void setId(Integer id) {
this.id = parentId;
}
}
@Entity
@Access(AccessType.FIELD)
public class Child extends Parent implements Serializable {
/*
What should be added to this entity to re assign the @Id to a new field and
make the parentId field just an ordianry field in the Child, not the entity Id
*/
@Id
private Long childId;
}
我曾尝试使用@AttributeOverride,但它所能提供的只是重命名 id 列名。
【问题讨论】:
-
我认为这是不可能的。那么它应该如何映射到RDB? “每个层次结构的单个表”和“每个子类的表”肯定是不可能的。为什么需要这个功能? (顺便说一句:为什么你的孩子继承父类?)
-
@isnot2bad 我有一个通用实体,它包含许多实体之间共享的 id、版本和其他一些字段。并且应用程序已经在构建时考虑了通用实体。但对于一些共享相同字段的新实体,除了 Id 字段类型和名称。但是他们仍然保留旧的 Id 列,但作为一个普通字段