[英]Referential integrity constraint violation: when deleting Entity in Hibernate (H2 in memory DBMS)
I am building my first Spring Boot application.我正在构建我的第一个 Spring 引导应用程序。 I use Hibernate and an H2 in-memory DBMS.我使用 Hibernate 和 H2 内存 DBMS。
What I am trying to build is a REST API that represents a number of App-Stores.我正在尝试构建的是一个 REST API ,它代表了许多应用商店。 I have an entity called App and another called Store.我有一个名为 App 的实体和另一个名为 Store 的实体。 A store can contain many apps and each app can be contained in more than one store.一个商店可以包含许多应用程序,每个应用程序可以包含在多个商店中。 Apps however, do not know in which stores they are contained.但是,应用程序不知道它们包含在哪些商店中。 I want to be able to delete apps and stores independently of each other.我希望能够相互独立地删除应用程序和商店。 Just because a store was deleted does not mean the apps therein should be deleted too and vice versa.仅仅因为商店被删除并不意味着其中的应用程序也应该被删除,反之亦然。 Apps can exist without being in a store and stores without apps are fine too.应用程序可以在没有商店的情况下存在,没有应用程序的商店也可以。
Here is the code for my entities, LpApp is the implementation for an App and LpTemplate is the implementation of a Store:这是我的实体的代码,LpApp 是 App 的实现,LpTemplate 是 Store 的实现:
@Entity
public class LpApp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long id;
@NotBlank(message = "An app needs a non-empty name")
@Column(nullable = false, updatable = false, unique = true)
private String appName;
// ... constructors, getters, setters, no further annotations
}
@Entity
public class LpTemplate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long id;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "template_apps",
inverseJoinColumns = { @JoinColumn(name = "app_id") },
joinColumns = { @JoinColumn(name = "template_id") })
private Set<LpApp> apps = new HashSet<>();
// ... constructors, getters, setters, no further annotations
}
This works well until I attempt to delete an App or Store from my DBMS.在我尝试从我的 DBMS 中删除应用程序或商店之前,此方法效果很好。 At this point I get an org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException.此时我得到一个 org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException。
The exception I get is the following (I trimmed the call stack for brevity):我得到的异常如下(为简洁起见,我修剪了调用堆栈):
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["APP_ID: PUBLIC.TEMPLATE_APPS FOREIGN KEY(APP_ID) REFERENCES PUBLIC.LP_APP(ID) (3)"; SQL statement:
delete from lp_app where id=? [23503-199]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
...
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
...
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "APP_ID: PUBLIC.TEMPLATE_APPS FOREIGN KEY(APP_ID) REFERENCES PUBLIC.LP_APP(ID) (3)"; SQL statement:
delete from lp_app where id=? [23503-199]
I am obviously doing something wrong, but I don't know where to look.我显然做错了什么,但我不知道在哪里看。 I guess I am not using the @ManyToMany annotation right or perhaps it is the wrong annotation for my use case.我想我没有正确使用 @ManyToMany 注释,或者它可能是我的用例的错误注释。
Thank you very much.非常感谢。
The short version of the answer is that you are trying to delete a record that has an existing relationship with another record.答案的简短版本是您正在尝试删除与另一条记录具有现有关系的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.