【发布时间】:2019-07-23 12:17:35
【问题描述】:
上下文
我有一个类Oeuvre,还有两个子类Livre 和Magasine。 Auteur 链接到 Livre。
所以类图看起来像:
Oeuvre
|
____________
| |
Magasine Livre____Auteur
实体
@Entity
public class Oeuvre {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String idoeuvre;
@NotNull
private String titre;
@NotNull
private String ISBN;
@ColumnDefault("CURRENT_DATE()")
private LocalDate parution;
}
@Entity
public class Magasine extends Oeuvre {
@Enumerated(EnumType.STRING)
private Type type;
}
@Entity
public class Livre extends Oeuvre {
@ManyToOne
@JoinColumn(name = "idauteur")
private Auteur auteur;
}
@Entity
public class Auteur {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String idauteur;
@NotNull
private String nom;
@NotNull
private String prenom;
}
H2 表示
Oeuvre(idoeuvre, isbn, parution, titre, type, idauteur)
但是通过这种表示,我可以有空值。例如,如果 Oeuvre 是 Magasine,idauteur 将是 null。
那么,这种表示方式不是更好吗?
Oeuvre(idoeuvre, isbn, parution, titre)
Livre(idlivre, idauteur, idoeuvre)
Magasine(idmagasine, type, idoeuvre)
SQL 脚本
使用当前的数据库方案,我无法创建此约束:ALTER TABLE livre ADD FOREIGN KEY (idauteur) REFERENCES auteur(idauteur) ON UPDATE CASCADE
结论
可以用JPA/H2来表示吗?
感谢您的回答。
编辑 1:问题
JPA/H2可以有这个数据库方案吗?
Oeuvre(idoeuvre, isbn, parution, titre)
Livre(idlivre, idauteur, idoeuvre)
Magasine(idmagasine, type, idoeuvre)
【问题讨论】:
-
你的问题描述让我有点困惑,你打算有一个名为
Oeuvre的表,还是有两个表,一个用于Livre,另一个用于@ 987654338@? -
说实话,我不知道最好的方法是什么,你能告诉我吗?
-
如果你不想要那些
null问题,并且你想为Livre定义一个约束,那么我认为你回答了你自己的问题,并且你想要一个单独的表来为@987654341 @ 和Magasine. -
我不能创建三个实体?
Oeuvre、Livre和Magasine?因为如果我只创建Livre和Magasine,我就不能像ALTER TABLE exemplaire ADD FOREIGN KEY (idoeuvre) REFERENCES oeuvre(idoeuvre) ON UPDATE CASCADE那样创建约束,对吧?Exemplaire是链接到Oeuvre的实体 -
@g00glen00b 我编辑了我的帖子。
标签: spring-boot jpa spring-data-jpa h2