gpt4 book ai didi

java - JPA实体: inheritance and one-to-one relationship not working simultaneously

转载 作者:行者123 更新时间:2023-12-01 12:05:46 24 4
gpt4 key购买 nike

我有以下方案:TableA1和TableA2存在于数据库中,每个都由一个实体bean表示。由于它们是相关的,我创建了一个抽象类(TableA,它是一个实体,但在数据库中不存在),其中两个实体都继承自该类。另外,TableA与TableB具有一对一的关系。

我的目标是查询 TableB,并根据类型从那里获取 TableA1 或 TableA2 的信息。

TableA1和TableA2各有一个id(每个表自动生成一个序号,因此可能会出现重复)。

在 TableB 中,我有两列组合起来代表外键:type 和 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

}

更新

我在寻找不可能的事情吗?这是我发现的:

Polymorphic relations to non-leaf classes in a table-per-class hierarchy have many limitations. When the concrete subclass is not known, the related object could be in any of the subclass tables, making joins through the relation impossible. This ambiguity also affects identity lookups and queries; these operations require multiple SQL SELECTs (one for each possible subclass), or a complex UNION.

更新2

数据库中已存在表A1、表A2和表B,并且具有以下结构:

 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)
);

最佳答案

更新答案:

已更新以匹配数据库

您可以使用 getDiscriminatorValue() 来访问 DiscriminatorValue。

像这样定义映射:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public abstract class TableA implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "surrogate_key")
protected int id;

@Id
@Column(name = "type")
protected int type;

// Constructors & getters/setters

@Transient
public String getDiscriminatorValue() {
DiscriminatorValue val = this.getClass().getAnnotation(DiscriminatorValue.class);
return val == null ? null : val.value();
}
}


@Entity
@DiscriminatorValue("1")
public class TableA1 extends TableA {

@Column(name = "some_char", length = 1)
private char someChar;

// Constructors & getters/setters & toString/equals
}


@Entity
@DiscriminatorValue("2")
public class TableA2 extends TableA {

@Column(name = "some_int")
private int someInt;

// Constructors & getters/setters & toString/equals
}

@Entity
public class TableB implements Serializable {
@Id
@GeneratedValue
@Column(name = "surrogate_key")
private int id;

@OneToOne
@Cascade(value = CascadeType.SAVE_UPDATE)
@JoinColumns({@JoinColumn(name = "sk", referencedColumnName = "surrogate_key"),
@JoinColumn(name = "type", referencedColumnName = "type")})
private TableA tableA;

@Column(name = "description", length = 200)
private String description;

// Constructors & getters/setters & toString/equals

}

并像这样查询:

newSession.createQuery("from TableB tb where tb.tableA.type=:type order by tb.id asc").setParameter("type", 1));

关于java - JPA实体: inheritance and one-to-one relationship not working simultaneously,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com