java - InheritanceType.JOINED、@PrimaryKeyJoinColumn 和嵌套子类

标签 java hibernate jpa

我刚开始使用 Hibernate,对 InheritanceType.JOINED 和 @PrimaryKeyJoinColumn 有疑问。

给定以下数据库表,其中员工引用人员,经理引用员工:

create table person (person_id int(10) auto_increment, 
                     name varchar(100),
                     primary key (person_id));

create table employee (employee_id int(10) auto_increment,
                       person_id int(10),
                       salary int(10),
                       primary key (employee_id));

create table manager (manager_id int(10) auto_increment,
                      employee_id int(10),
                      shares int(10),
                      primary key (manager_id));

我可以为 Person 和 Employee 创建前两个类,如下所示:

@Entity
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {

    @Id
    @GeneratedValue
    @Column(name="person_id")
    private int personId;

    @Column(name="name")
    private String name;
}


@Entity
@Table(name="employee")
@PrimaryKeyJoinColumn(name="person_id")
public class Employee extends Person {

    @GeneratedValue
    @Column(name="employee_id")
    private int employeeId;

    @Column(name="salary")
    private int salary;
}

当涉及到 Manager 类时,我不确定为 @PrimaryKeyJoinColumn 使用哪个值。通过阅读 JPA 规范,我应该在整个父类(super class)/子类/子类层次结构中只使用一个 id,我怀疑它应该是 person_id。

我是否应该从经理表中删除 employee_id 列并将其替换为 person_id?我问的原因是在数据库中有“级联”主键似乎很自然(即经理不直接指人)但我相信 Hibernate 要求所有子子类(无论“深”)包含一个外键回到父类(super class)。对我的断言进行一些确认将非常有用。

最佳答案

您可以随意使用列名。 @PrimaryKeyJoinColumn 注释用于告诉hibernate 连接表中外键列的名称是什么。

@Entity
@Table(name = "manager")
@PrimaryKeyJoinColumn(name = "employee_id")
public class Manager extends Employee{
    private String branch;
}

这段代码意味着在 manager 表中有一个名为 employee_id 的列,它是 employee 表的外键。

这个注释是可选的,所以如果你不提供它那么外键列名将与基类中的键名相同(如果多级继承则为 super 基),在你的情况下如果你不提供则任何子类都将 person_id 作为外键。

如果您想在 manager 表中将字段名称保留为 employee_id,则必须这样提供。

@PrimaryKeyJoinColumn(name="employee_id")

如果您根本不提供此注释,那么将有一个列名 person_id 用作 employee 表的外键。

这是文档所说的内容。

This annotation specifies a primary key column that is used as a foreign key to join to another table.

It is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.

If no PrimaryKeyJoinColumn annotation is specified for a subclass in the JOINED mapping strategy, the foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass

编辑

当我尝试通过 id 检索管理器时,Hibernate 正在执行的查询。

select
            manager0_.id as id1_4_0_,
            manager0_2_.name as name2_4_0_,
            manager0_1_.employee_id as employee1_1_0_,
            manager0_1_.salary as salary2_1_0_,
            manager0_.branch as branch1_2_0_ 
        from
            manager manager0_ 
        inner join
            employee manager0_1_ 
                on manager0_.id=manager0_1_.id 
        inner join
            person manager0_2_ 
                on manager0_.id=manager0_2_.id 
        where
            manager0_.id=?

如您所见,manager 有一个 employee 而不是 person 表的外键。我还没有创建表。我使用了 hbm2ddl=create

这意味着 Hibernate 本身使用级联主键创建表,而不仅仅是 super 父键。

我理解你的困惑,即使我在 hibernate 中也找不到任何东西documentation .它只是说 FK 将指向父类(super class)主键。它不指定直接父类(super class)或顶级父类(super class)。 虽然我检查了其他 JPA 实现,即 OpenJPA .它确实明确指出 FK 指向直接父类(super class)。

关于java - InheritanceType.JOINED、@PrimaryKeyJoinColumn 和嵌套子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145697/

相关文章:

java - 如何在多线程JAVA环境中保护对象而不损失性能?

java - 如何使用 map 中的键/值制作 JSON 字符串

java - 过滤掉 JPA 查询中的嵌套对象

java.time.LocalDateTime 在带有 hibernate 的 JPA 中被持久化为tinyblob而不是 TemporalType

java - 批量插入多对多关系

java - EclipseLink:检查当前事务隔离级别

Java 内存空间 - 它们位于何处?

java - Switch Statement输出数据到新的Var中

spring - TransactionManager无法初始化

java - 在 Hibernate 中通过 ManyToOne 关系获取 Order