[英]JPA joined table inheritance and full data integrity
Consider the following JPA joined table inheritance example from here考虑以下 JPA 连接表 inheritance 示例从这里
Java code: 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 code: 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)
They say in the article他们在文章中说
The JOINED table inheritance strategy addresses the data integrity concerns because every subclass is associated with a different table.
JOINED 表 inheritance 策略解决了数据完整性问题,因为每个子类都与不同的表相关联。
However, as I understand in this example we provide only one half of data integrity protection at the level of DB.但是,正如我在此示例中所了解的,我们仅在 DB 级别提供了一半的数据完整性保护。 For example, we can't delete Shape and leave Circle/Rectangle but we can delete Circle/Rectangle and leave Shape.
例如,我们不能删除Shape并离开Circle/Rectangle,但我们可以删除Circle/Rectangle并离开Shape。 So, as I understand we don't have 100% data integrity protection.
因此,据我了解,我们没有 100% 的数据完整性保护。
Is there a way to provide full data integrity protection with joined table inheritance in JPA/Hibernate?有没有办法通过 JPA/Hibernate 中的连接表 inheritance 提供完整的数据完整性保护?
If your premise is that somebody will delete the data with a query in the wrong table, then no, there isn't.如果您的前提是有人会在错误的表中使用查询删除数据,那么不,没有。
But if you delete an entity using entityManager.remove(...)
, Hibernate ORM will delete the rows from the right tables.但是,如果您使用
entityManager.remove(...)
删除实体, Hibernate ORM 将从右表中删除行。
The problem is that the table Shape
cannot have a single foreign key constraint that refers to 2 different tables (as far as I know).问题是表
Shape
不能有一个引用 2 个不同表的外键约束(据我所知)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.