我已经实现了一个由 Article 和 Fruit 表组成的数据库,具有以下规格:
create table Fruit
(
ART_ID bigint,
FRU_ID bigint not null auto_increment,
FRU_FROZEN varchar(15),
primary key(FRU_ID)
);
# Implemented
create table Article
(
ART_ID bigint,
ART_NAME varchar(10) not null,
ART_COST varchar(10) not null,
primary key(ART_ID)
);
更改表水果添加约束 FK_FRUIT_ARTICLE 外键 (ART_ID) 引用文章 (ART_ID) 上删除限制更新限制;
并具有以下类实体:Article.java
@Entity
@Table(name = "Article")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Article implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "ART_ID")
private Long id;
@Basic(optional = false)
@Column(name = "ART_NAME")
private String name;
@Basic(optional = true)
@Column(name = "ART_COST")
private String cost;
// 构造函数、getter 和 setter
水果.java
@Entity
@Table(name="Fruit")
public class Fruit extends Article{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "FRU_ID")
private long fruitID;
@Basic(optional = false)
@Column(name = "FRU_FROZEN")
private String fruitFrozen;
// 构造函数、getter 和 setter
现在,我的问题是如何在 Article 和 Fruit 中拥有 ID,如果我保持这种方式,它会抛出异常,即子类不能包含 IDClass,因为它会导致多个 ID。任何帮助表示赞赏。提前致谢。