问题描述
关于这个主题的大量 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 (将#修改为@)