[英]JPA joined table inheritance and full data integrity
考慮以下 JPA 連接表 inheritance 示例從這里
Java代碼:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Shape {
@Id
@Column(name = "vehicle_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
@Entity
public class Circle extends Shape {
private double radius;
+ constructor/getters/setters
}
@Entity
public class Rectangle extends Shape {
private double width;
private double length;
+ constructor/getters/setters
}
SQL 代碼:
create table Circle (
radius double precision not null,
shape_id integer not null,
primary key (shape_id)
)
create table Rectangle (
length double precision not null,
width double precision not null,
shape_id integer not null,
primary key (shape_id)
)
create table Shape (
shape_id integer not null auto_increment,
primary key (shape_id)
)
alter table Circle
add constraint FK2nshngrop6dt5amv1egecvdnn
foreign key (shape_id)
references Shape (shape_id)
alter table Rectangle
add constraint FKh3gkuyk86e8sfl6ilsulitcm5
foreign key (shape_id)
references Shape (shape_id)
他們在文章中說
JOINED 表 inheritance 策略解決了數據完整性問題,因為每個子類都與不同的表相關聯。
但是,正如我在此示例中所了解的,我們僅在 DB 級別提供了一半的數據完整性保護。 例如,我們不能刪除Shape並離開Circle/Rectangle,但我們可以刪除Circle/Rectangle並離開Shape。 因此,據我了解,我們沒有 100% 的數據完整性保護。
有沒有辦法通過 JPA/Hibernate 中的連接表 inheritance 提供完整的數據完整性保護?
如果您的前提是有人會在錯誤的表中使用查詢刪除數據,那么不,沒有。
但是,如果您使用entityManager.remove(...)
刪除實體, Hibernate ORM 將從右表中刪除行。
問題是表Shape
不能有一個引用 2 個不同表的外鍵約束(據我所知)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.