在更新和/或删除时违反休眠H2参照完整性约束
来源:爱站网时间:2021-09-16编辑:网友分享
因此,我目前正在为大学项目建立数据库。在数据库中,我们将有一个引用用户的对象。用户应该能够自由添加,编辑和删除条目...
问题描述
因此,我目前正在为大学项目建立数据库。在数据库中,我们将有一个引用用户的对象。用户应该能够自由地从数据库中添加,编辑和删除条目。
只要用户未分配要使用的实验工作站,我便可以创建,编辑和删除用户。一旦将它们分配给一个,我就开始收到错误:
Referential integrity constraint violation: "FKGQN2NFAK1N6TWN592B0KQ34IT: PUBLIC.EXPERIMENTIERSTATION_USER FOREIGN KEY(BENUTZER_ID) REFERENCES PUBLIC.USER(ID) (.......)"; SQL statement:
delete from User where id=?
现在,我确实了解为什么会这样,因此,为什么ExperimentingStation
属性引用了User,反之亦然。但是,即使将CascadeType
设置为MERGE
,我仍然会收到错误消息,我不太了解。我们使用的是Hibernate的默认配置,因此我知道我们不必担心表名,这就是为什么它们不包含在以下代码中的原因,但是,我不知道如何在不使用名称的情况下使用mappedBy
。
另一个问题是我们试图在ExperimentingStation
中保留一个队列,但该队列也无法正常工作。
任何帮助将不胜感激!
非常感谢
代码:
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 status;
/** Conditions for using an experimenting station */
@OneToMany
private List bedingungen;
@NonNull
@ManyToMany(cascade=CascadeType.MERGE, fetch = FetchType.LAZY)
private List 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 rollen;
/**
* The User's language preference
*/
@NonNull
private String language;
public String toString() {
return vorname + nachname;
}
}
思路:
[经过深思熟虑,我们决定不从实体管理器中完全删除对象,而是只向每个元素添加一个VALID-boolean,而仅从数据库中获取Valid元素,这可以正常工作。
上一篇:@@ GetMapping和@GetMapping(“ / {id}”)始终返回@GetMapping(“ / {id}”)
下一篇:如何在Spring Boot 2.2.4中与@ConfigurationProperties一起使用@ConstructorBinding和@PropertySource?