Hibernate-继承中的外键和主键 [英] Hibernate - Foreign and primary keys in inheritance

查看:59
本文介绍了Hibernate-继承中的外键和主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否可能会有这样的事情.假设我有一个名为 material 的表:

I was wondering, is it possible to have something like this. Let's say I have a table called 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端,实体是:

On Java side entity would be:

@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端,实体是:

On Java side entity would be:

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

    private String name;

    private BigDecimal price;

    @Enumerated(EnumType.STRING)
    private PaymentType paymentType;

    // Getters and setters

}

现在让我说我想在Java端为职业材料创建一个通用类,称为 Expense ,它将代表材料费用和职业.看起来像这样:

Now lets say I want to create a common class for an Occupation and Material called Expense on Java side that will represent expenses of materials and occupations. That would look something like this:

@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 |
+--------------+-------------+------+-----+---------+----------------+

但是,问题是当我们引入继承时,费用的主键是物料和占用表中的外键.费用表是否可能有其自己的主键,并以某种方式持有物料和职业的ID和鉴别符类型,以便物料和职业表可以具有具有相同ID的行(例如物料中的 34 id和<占领表中的code> 34 id)?我的意思是,在SQL方面可以很容易地完成,但是Java方面又如何呢?

However, the problem is that primary key of expenses is a foreign key in material and occupation table when we introduce inheritance. Is it possible that expense table has its own primary key and to somehow hold id's and discriminator types for materials and occupations so that material and occupation tables can have rows with same id's (like 34 id in material and 34 id in occupation table)? I mean, on SQL side it can be done quite easy, but what about Java side?

推荐答案

根据休眠文档,尝试:

@PrimaryKeyJoinColumn和@PrimaryKeyJoinColumns批注定义联接的子类表的主键:

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/zh-CN/html_single/#d0e1168

还要查看PrimaryKeyJoinColumn javadoc,它具有:

Also look at PrimaryKeyJoinColumn javadoc, it has:

(可选)正在使用的表的主键列的名称加入.默认名称与的主键列的名称相同超类的主表( JOINED 映射战略);与主键的主键列名称相同表( SecondaryTable 映射);或与引用实体表的主键列( OneToOne 映射).

(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).

字符串referencedColumnName()

String referencedColumnName()

我认为正是您所需要的

这篇关于Hibernate-继承中的外键和主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆