hibernate - JPA:是否可以更改继承层次结构中的 PK?

标签 hibernate inheritance jpa mapping primary-key

我有以下数据库设计:

Valid XHTML
(来源: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/

相关文章:

java - JPA/Hibernate动态注入(inject)实体管理器

spring - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常 : JDBC commit failed

java - 具有常规数据库操作的多用户 Web 应用程序

c++ - 通过运行时索引访问派生类中基类的成员

C++ - 错误 : expected unqualified-id before ‘using’

java - spring data jpa @Query 枚举类型不返回数据

java - 如何使用 JPA 更新带有 Google App Engine Java 的实体?

java - 将 Hibernate 查询结果存储到 Hashmap 中

java - 在save()之前如何停止Spring数据JPA进行SELECT?

c++ - 尝试从 C++ 中的构造函数继承类时出错