简体   繁体   English

违反参照完整性约束:删除 Hibernate 中的实体时(memory DBMS 中的 H2)

[英]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.非常感谢。

You need to add cascade attribute to tell hibernate not to delete the entity on delete operation.您需要添加级联属性来告诉 hibernate 在删除操作时不要删除实体。 There are different options for cascade.级联有不同的选项。 Refer to this link to understand the different options.请参阅链接以了解不同的选项。

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.

相关问题 更新和/或删除时违反 Hibernate H2 参照完整性约束 - Hibernate H2 Referential Integrity Constraint Violation on Update and/or Remove Hibernate / H2 @OneToMany移除子代时违反“参照完整性约束”? - Hibernate/H2 @OneToMany “Referential integrity constraint violation” on remove of child? H2参照完整性约束违反 - H2 Referential integrity constraint violation 当删除带有播放框架2.2.x的对象时,违反引用完整性约束 - Referential integrity constraint violation when deleting an object with play-framework 2.2.x 参照完整性约束违规 - 添加超过2条记录时失败 - Referential integrity constraint violation - failing when add more then 2 records 无继承关系的参照完整性约束违规 - Referential integrity constraint violation for none inheritance relationship 如何防止在测试中违反参照完整性约束? - How to prevent referential integrity constraint violation in tests? 尝试从 hibernate 中与 cascadeType.ALL 的 OneToMany 关系中删除时出现参照完整性约束违规 - Getting referential integrity constraint violation while trying to delete from OneToMany relation with cascadeType.ALL in hibernate Hibernate Cascade DELETE OneToMany 不起作用。 违反参照完整性约束 - Hibernate Cascade DELETE OneToMany does not work. Referential integrity constraint violation JPA 2:在保存具有无方向OneToMany关系的实体时违反了引用完整性约束 - JPA 2: Referential integrity constraint violation while saving entity with undirectional OneToMany relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM