java - Hibernate - 继承中的外键和主键

标签 java mysql hibernate

我在想,有没有可能有这样的东西。假设我有一个名为 material 的表:

+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id    | int(11)       | NO   | PRI | NULL    |       |
| name  | varchar(255)  | YES  |     | NULL    |       |
| price | decimal(19,2) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+

在 Java 端实体将是:

@Entity
@DiscriminatorValue("M")
public class Material extends Expense {

    private String name;

    private BigDecimal price;

    // Getters and setters
}

和名为 occupation 的表:

+--------------+---------------+------+-----+---------+-------+
| Field        | Type          | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| id           | int(11)       | NO   | PRI | NULL    |       |
| name         | varchar(255)  | YES  |     | NULL    |       |
| payment_type | varchar(255)  | YES  |     | NULL    |       |
| price        | decimal(19,2) | YES  |     | NULL    |       |
+--------------+---------------+------+-----+---------+-------+

在 Java 端实体将是:

@Entity
@DiscriminatorValue("O")
public class Occupation extends Expense {

    private String name;

    private BigDecimal price;

    @Enumerated(EnumType.STRING)
    private PaymentType paymentType;

    // Getters and setters

}

现在假设我想在 Java 端为 OccupationMaterial 创建一个名为 Expense 的公共(public)类,它将代表 Material 的费用和职业。这看起来像这样:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "expense_type", discriminatorType = DiscriminatorType.STRING)
public class Expense {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    // Getters and setters

}

在 SQL 方面:

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| expense_type | varchar(31) | NO   |     | NULL    |                |
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
+--------------+-------------+------+-----+---------+----------------+

但是问题是我们引入继承时,expenses的主键是material和occupation表的外键。费用表是否有可能有自己的主键,并以某种方式保存 Material 和职业的 id 和鉴别器类型,以便 Material 和职业表可以具有具有相同 id 的行(如 Material 中的 34 id 和 34 职业表中的 id)?我的意思是,在 SQL 端它可以很容易地完成,但是在 Java 端呢?

最佳答案

根据 hibernate 文档,尝试:

The @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations define the primary key(s) of the joined subclass table:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Boat implements Serializable { ... }

@Entity
@PrimaryKeyJoinColumn(name="BOAT_ID")
public class AmericaCupClass  extends Boat { ... }    

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168

另请参阅 PrimaryKeyJoinColumn javadoc,它有:

(Optional) The name of the primary key column of the table being joined to. Defaults to the same name as the primary key column of the primary table of the superclass (JOINED mapping strategy); the same name as the primary key column of the primary table (SecondaryTable mapping); or the same name as the primary key column for the table for the referencing entity (OneToOne mapping).

String referencedColumnName()

我想这就是你需要的

关于java - Hibernate - 继承中的外键和主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732180/

相关文章:

java - 如何检索用户 session 属性,例如用户名?

php - 如何以不同的样式显示php echo结果?

hibernate - jboss 7.2 与 primefaces 和 hibernate 的兼容性

hibernate.hbm2ddl.auto=创建不清除现有数据

java - 检查大于 0 的整数

java - 从 void 方法写入文件

php - PHP/PDO 下的 MySQL 槽

php - 从 MYSQL 过滤器创建动态多维数组

java - 从主表实体引用主数据实体的外键列数错误

java - Android - 将 ImageView 设置为 URL