【发布时间】:2020-06-01 19:31:13
【问题描述】:
我想删除一个引用“Authority”实例的“UserRegistration”实例。通过存储库删除“UserRegistration”实例时,出现以下错误:
“无法删除或更新父行:外键约束失败 (seprojekt.authorities, CONSTRAINT authorities_ibfk_1 FOREIGN KEY (userid) REFERENCES users (id))”
删除“UserRegistration”实例时是否有可能删除引用的实体?我在“UserRegistration”中使用 Cascadetype 和 orphanRemoval 进行了尝试,但没有任何反应。
尝试删除实体的方法:
@DeleteMapping
public boolean deleteUser(@RequestParam String username) {
Optional<UserRegistration> optUserRegistration = uRepo.findByUsername(username);
if(optUserRegistration.isPresent()) {
System.out.println("!!! Authority is: '" + optUserRegistration.get().getAuthority().getAuthority());
uRepo.delete(optUserRegistration.get());
return true;
}
return false;
}
UserRegistration 类如下所示:
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "users")
public class UserRegistration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
@JsonIgnore
private String password;
@JsonIgnore
private boolean enabled;
private Integer profilePicture;
private String email;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id")
private Authority authority;
@JsonManagedReference
@ManyToMany
@JoinTable(name = "userhasfavorite", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "channelid"))
private List<Channel> favorites;
public UserRegistration() {
}
public UserRegistration(String email, String username, String password, boolean enabled, int profilePicture) {
this.email = email;
this.username = username;
this.password = password;
this.enabled = enabled;
this.profilePicture = profilePicture;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Integer getProfilePicture() {
return profilePicture;
}
public void setProfilePicture(Integer profilePicture) {
this.profilePicture = profilePicture;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
public List<Channel> getFavorites() {
return favorites;
}
public void setFavorites(List<Channel> favorites) {
this.favorites = favorites;
}
}
权威类看起来像这样:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "authorities")
public class Authority {
@Id
@Column(name = "userid")
private int userID;
private String username;
private String authority;
@OneToOne(mappedBy = "authority")
private UserRegistration user;
public Authority() {
}
public Authority(int userID, String username, String authority) {
this.userID = userID;
this.username = username;
this.authority = authority;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public UserRegistration getUser() {
return user;
}
public void setUser(UserRegistration user) {
this.user = user;
}
}
非常感谢您阅读本文。如果需要,请向我询问更多详细信息。
【问题讨论】:
标签: java spring hibernate jpa orm