简体   繁体   English

更新和/或删除时违反 Hibernate H2 参照完整性约束

[英]Hibernate H2 Referential Integrity Constraint Violation on Update and/or Remove

So I am currently building a database for a university project.所以我目前正在为一个大学项目建立一个数据库。 In the database, we are going to have an object that references a user.在数据库中,我们将有一个引用用户的对象。 Users should be able to freely add, edit, and remove entries from the database.用户应该能够自由地添加、编辑和删除数据库中的条目。

I am able to create, edit, and delete users, so long as they are not assigned an experimenting station where they will be working.我可以创建、编辑和删除用户,只要他们没有被分配到他们将在其中工作的试验站。 As soon as they are assigned to one, I start getting the error:一旦将它们分配给一个,我就开始收到错误消息:

Referential integrity constraint violation: "FKGQN2NFAK1N6TWN592B0KQ34IT: PUBLIC.EXPERIMENTIERSTATION_USER FOREIGN KEY(BENUTZER_ID) REFERENCES PUBLIC.USER(ID) (.......)"; SQL statement:
delete from User where id=?

Now I do kind of understand why this is happening, hence why the ExperimentingStation attribute has a reference to the User and vice versa.现在我有点理解为什么会发生这种情况,因此为什么ExperimentingStation属性有对用户的引用,反之亦然。 However, even when setting the CascadeType to MERGE , I still get the error, which I do not quite understand.但是,即使将CascadeType设置为MERGE ,我仍然收到错误,我不太明白。 We are using Hibernate with it's default configuration, so I understand we do not need to worry about the Table names, which is why they are not included in the following code, however, I do not know how to use mappedBy without the names.我们正在使用 Hibernate 的默认配置,所以我知道我们不需要担心表名称,这就是为什么它们不包含在以下代码中的原因,但是,我不知道如何在没有名称的情况下使用mappedBy

Another issue is that we are trying to persist a Queue within ExperimentingStation which also isn't working.另一个问题是我们试图在ExperimentingStation保留一个队列,但它也不起作用。

Any help would be appreciated!任何帮助,将不胜感激!

Thanks a lot in advance非常感谢提前

The code:编码:

Class ExperimentierStation:

    import lombok.*;

    import javax.persistence.*;
    import java.util.List;
    import java.util.Queue;

    /** Experimenting stations data class */
    @Data
    @Entity
    @NamedQueries({
        @NamedQuery(name = "ExperimentierStation.findAllInLocation",
                query = "SELECT es FROM ExperimentierStation es WHERE es.standort = :standort"),
        @NamedQuery(name = "ExperimentierStation.getByStatus",
                query = "SELECT es FROM ExperimentierStation es WHERE es.status = :status"),
        @NamedQuery(name = "ExperimentierStation.getAll", query = "SELECT es FROM ExperimentierStation es")
    })
    @RequiredArgsConstructor
    @NoArgsConstructor
    public class ExperimentierStation {

    /** The station's id */
    @NonNull
    @Id
    private int esID;

    /** The station's location */
    @NonNull
    @ManyToOne
    private Standort standort;

    @NonNull
    private String name;

    /** The station's status */
    @NonNull
    private Enum<ExperimentierStationZustand> status;

    /** Conditions for using an experimenting station */
    @OneToMany
    private List<Bedingung> bedingungen;

    @NonNull
    @ManyToMany(cascade=CascadeType.MERGE, fetch = FetchType.LAZY)
    private List<User> benutzer;

    @OneToOne
    private ProzessSchritt currentPS;
    }

Class User:

    import lombok.*;

    import javax.persistence.*;
    import javax.ws.rs.Path;
    import java.time.LocalDateTime;
    import java.util.List;

    /**
     * This class is used to create user objects
     */
    @Data
    @Entity
    @NamedQueries({
        @NamedQuery(name = "User.findById", query = "SELECT u from User u WHERE u.id = :id"),
        @NamedQuery(name = "User.findByUsername", query = "SELECT u from User u WHERE u.username = :username"),
        @NamedQuery(name = "User.findByEmail", query = "SELECT u from User u WHERE u.email = :email"),
        @NamedQuery(name = "User.getAll", query = "SELECT u FROM User u")
    })
    @RequiredArgsConstructor(access = AccessLevel.PUBLIC)
    @NoArgsConstructor(access = AccessLevel.PUBLIC)
    public class User {

    /**
     * User's id
     */
    @Id
    @NonNull
    private int id;

    /**
     * The user's name
     */
    @NonNull
    private String vorname;

    /**
     * The user's surname
     */
    @NonNull
    private String nachname;

    /**
     * User's email address
     */
    @NonNull
    private String email;

    /**
     * The user's phone number
     */
    @NonNull
    private String telefonnummer;

    /**
     * User's username
     */
    @NonNull
    private String username;

    /**
     * User's hashed password
     */
    @NonNull
    private String password;

    /**
     * Is the user verified?
     */
    @NonNull
    private boolean wurdeVerifiziert;

    /**
     * Creation date of the user object
     */
    @NonNull
    private LocalDateTime erstellungsDatum;

    /**
     * The role's of the user
     */
    @NonNull
    @ElementCollection
    private List<Role> rollen;


    /**
     * The User's language preference
     */
    @NonNull
    private String language;

    public String toString() {
        return vorname + nachname;
    }
}

经过深思熟虑,我们决定不完全从实体管理器中删除对象,而只是向每个元素添加一个 VALID-boolean,并且只从数据库中获取 Valid 元素,这可以完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate / H2 @OneToMany移除子代时违反“参照完整性约束”? - Hibernate/H2 @OneToMany “Referential integrity constraint violation” on remove of child? H2参照完整性约束违反 - H2 Referential integrity constraint violation 违反参照完整性约束:删除 Hibernate 中的实体时(memory DBMS 中的 H2) - Referential integrity constraint violation: when deleting Entity in Hibernate (H2 in memory DBMS) 无继承关系的参照完整性约束违规 - 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 参照完整性约束违规 - 添加超过2条记录时失败 - Referential integrity constraint violation - failing when add more then 2 records 所有者删除时违反参照完整性约束(OneToMany单向) - Referential integrity constraint violation on owner delete (OneToMany unidirectional) Spring 引导 - 测试存储库 - 违反参照完整性约束 - Spring Boot - testing repository - Referential integrity constraint violation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM