【发布时间】:2014-12-23 16:36:56
【问题描述】:
我有以下方案:TableA1 和 TableA2 存在于数据库中,每个都由一个实体 bean 表示。由于它们是相关的,我创建了一个抽象类(TableA,它是一个实体,但在数据库中不存在),其中两个实体都继承自这个类。另外,TableA 与TableB 是一对一的关系。
我的目标是查询 TableB 并根据类型从那里获取 TableA1 或 TableA2 的信息。
TableA1和TableA2各有一个id(每个表都会自动生成一个序号,所以可能会有重复)。
在 TableB 中,我有两列组合起来表示外键:类型和 id。 Type = 1 表示 id 在 TableA1 中。与 TableA2 类似。
我的问题是我不知道如何将这两列定义为外部外键。 这就是我所拥有的:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="type")
public abstract class TableA {
@Id
@Column(name = "type")
protected int type;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
protected int id;
@Column(name = "name")
private String name;
// Getters and setters
}
@Entity
@DiscriminatorValue("1")
@Table (name="tableA1")
public class TableA1 extends TableA {
@Column(name="col1")
private String col1;
// Getters and setters
}
@Entity
@DiscriminatorValue("2")
@Table (name="tableA2")
public class TableA2 extends TableA {
@Column(name="col2")
private String col2;
// Getters and setters
}
@Entity
@Table (name="tableB")
public class TableB {
@Id
@Column(name="someId")
private Integer someId;
@Column(name="type")
private int type;
@Column(name="id")
private Integer id;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "type"),
@JoinColumn(name = "id" )
})
private TableA tableA;
// Getters and setters
}
更新
我在寻找不可能的事吗?这是我发现的:
在每类表的层次结构中与非叶类的多态关系有很多限制。当具体子类未知时,相关对象可能位于任何子类表中,从而无法通过关系进行连接。这种模糊性也会影响身份查找和查询;这些操作需要多个 SQL SELECT(每个可能的子类一个)或一个复杂的 UNION。
更新 2
TableA1、TableA2、TableB已经存在在数据库中,结构如下:
CREATE TABLE TableA1 (
surrogate_key int AUTO_INCREMENT,
some_char char(30),
PRIMARY KEY (surrogate_key)
);
CREATE TABLE TableA2 (
surrogate_key int AUTO_INCREMENT,
some_int int,
PRIMARY KEY (surrogate_key)
);
CREATE TABLE TableB (
surrogate_key int AUTO_INCREMENT,
type int, // if type=1, sk represents the surrogate_key of tableA1
// if type=2, sk represents the surrogate_key of tableA2
sk int,
description varchar(200),
PRIMARY KEY (surrogate_key)
);
【问题讨论】:
-
也许可以添加一个 EER 图,这样我们就可以理解这个复杂的休眠问题。
-
是的,如果 type=1,则 sk 指向 TableA1,如果 type=2,则 sk 指向 TableA2,如果 type = 0,则 sk = 0。这就是我尝试使用 @987654323 的原因@
-
我只见过 Discriminator 用于单表继承。您的表
TableA1和TableA2不是缺少类型列吗?还是 Hibernate 只是在内部使用它们? -
如果我将 type 列添加到 TableA1 和 TableA2 那么这些列将具有常量值(分别为“1”和“2”)。我试图避免这种情况。
-
您的限制是什么?表结构是否已冻结或 JPA 实体代码是否已冻结?你想达到什么目的?