由于在我开始这个项目之前就存在的原因,有些表具有相似的类型,但具有不同的 ID 列。
所以,当我尝试这个
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Element implements Serializable {
public String title;
}
@Entity
public class PrimaryElement extends Element {
@Id
long pid;
}
@Entity
public class OtherElement extends Element {
@Id
long oid;
}
但是后来我得到了一个明显的错误
No identifier specified for entity: Element
现在,我不能很好地将 ID 放入 Element 类中,因为它们显然映射到不同的列。
我尝试过各种风格的@Id和'abstract'以及@MappedSuperClass等等..
我完全迷失了。有办法解决这个问题吗?
任何见解将不胜感激。
谢谢!
最佳答案
您可以用 @MappedSuperclass 替换 Element 类的 @Entity 和 @Inheritance 注释(此注释负责技术映射,只有 PrimaryElement 和 OtherElement 才是功能齐全的实体),或者使用 @Id 注释移动 oid 字段到类 Element 并在其子类中使用 @AttributeOverride 注释来修改列名称(在这种情况下,抽象类 Element 也将是一个功能齐全的实体)。
@AttributeOverride(name="oid", column=@Column(name="primary_element_id"))
更新:
@MappedSuperclass
public abstract class Element implements Serializable {
@Id
private long id;
public String title;
}
@Entity
@AttributeOverride(name="id", column=@Column(name="pid"))
public class PrimaryElement extends Element {
}
@Entity
@AttributeOverride(name="id", column=@Column(name="oid"))
public class OtherElement extends Element {
}
关于java - Hibernate + JPA无法为子类映射不同的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32142718/