如何解决Spring Boot java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败
关于这个主题的大量 stackoverflow 问题并没有帮助我解决这个错误。
我正在学习 sprint-boot 安全教程,其中我试图完成的目标是注册一个新用户并测试新用户的身份验证。
有人可以帮我解决这个问题吗?
错误:
java.sql.sqlIntegrityConstraintViolationException: 无法添加或更新子行:外键约束失败 (polling_app
.user_roles
,CONSTRAINT FK55itppkw3i07do3h7qoclqd4k
FOREIGN KEY (user_id
)参考文献 user
(id
))
在 com.MysqL.cj.jdbc.exceptions.sqlError.createsqlException(sqlError.java:117) ~[mysql-connector-java-8.0.22.jar:8.0.22]
日期审核 - 父类:
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
value = {"createdAt","updatedAt"},allowGetters = true
)
public class DateAudit implements Serializable {
@CreatedDate
@Column(nullable = false,updatable = false)
private Instant createdAt;
@LastModifiedDate
@Column(nullable = false)
private Instant updatedAt;
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
public Instant getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
}
用户 - 模型类:
@Entity
@Table(name = "users",uniqueConstraints = {
@UniqueConstraint(columnNames = {
"username"
}),@UniqueConstraint(columnNames = {
"email"
})
})
public class User extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 40)
private String name;
@NotBlank
@Size(max = 15)
private String username;
@NaturalId
@NotBlank
@Size(max = 40)
@Email
private String email;
@NotBlank
@Size(max = 100)
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles",joinColumns = @JoinColumn(name = "user_id"),inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
public User() {
}
public User(String name,String username,String email,String password) {
this.name = name;
this.username = username;
this.email = email;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getpassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
此外,这是我的 docker 容器中的表的图像
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。