我有以下数据库设计:
(来源:kawoolutions.com)
如您所见,States、Countries 和 Continents 都是 GeoAreas 的子表。这里有趣的事实是,States 和 Countries 更改了它们的主键,而不是(据推测)从 GeoAreas 继承的主键。
目的:州和国家也用于邮政地址,我不想为此使用 GeoAreas.id(未显示对州和国家的引用)。
问题是: JPA支持这样的设计吗?仅 JPA 2.0?完全没有?
我看到的问题是如何将 @Id 注释(用于所有映射)指定为 GeoArea 类看起来像
public abstract class GeoArea implements Serializable
{
@Id
@Column(name = "id")
protected Integer id;
...
}
并且子类还需要在某些属性上指定另一个替代@Id,例如
public class Country extends GeoArea
{
@Id
@Column(name = "iso_code")
private String isoCode;
...
}
如果有,谁能指出 JPA 规范拒绝这种情况?
最佳答案
第 2.4 节
The primary key must be defined on the entity class that is the root of the entity hierarchy or on a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy. The primary key must be defined exactly once in an entity hierarchy.
即您不能在子类中“重新定义”标识(通过向标识添加额外字段)。
关于hibernate - JPA:是否可以更改继承层次结构中的 PK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5744822/