java - org.springframework.dao.DataIntegrityViolationException 在使用 hibernate 和 spring-boot-data-jpa 将 spring-boot 从 2.1.x 更新到 2.2.x 之后
问题描述
当尝试使用 JPA 将多个对象插入 MySQL 时,尝试将 spring-boot 从版本 2.1.12 更新到 2.2.4 时卡在 DataIntegrityViolationException 上。
示例对象:
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
@Table(name = "user")public class User {
@Id
@Column(name = "id")
@JsonProperty("id")
private String id;
@PrimaryKeyJoinColumn
@OneToOne(cascade = CascadeType.ALL)
@JsonProperty("status")
private UserStatus status;
}
和用户状态:
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "user_status")
public class UserStatus {
@Id
@Column(name = "id")
@JsonProperty("id")
private String id;
public UserStatus(String userId) {
this.id = userId;
}
}
要将对象插入 mysql,我使用默认的 jpa 存储库:
@Repository
public interface UserRepository extends JpaRepository<User, String> {
}
使用 spring-boot-2.1.xuserRepository.save(user)
可以正常工作,但使用 2.2.x 会引发此异常:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
在日志中使用此详细信息:
Cannot add or update a child row: a foreign key constraint fails (`test`.`user_status`, CONSTRAINT `user_status_ibfk_1` FOREIGN KEY (`id`) REFERENCES `user` (`id`) ON DELETE CASCADE)
如果启用spring.jpa.show-SQL: true
,我发现使用 spring-boot-2.2.x 时不会插入User
实体,但使用旧 spring 时会插入。
我没有发现 spring-boot 连接到 hibernate 有任何重大变化,并且在相应的更新之后 hibernate 本身也没有重大变化。有没有在发行说明中描述的更新?
解决方案
spring-boot 2.1.12 使用 Hibernate 5.3.15.Final 和 spring-boot 2.2.4 使用 Hibernate 5.4.10.Final
您的问题似乎类似于 Hibernate 问题HHH-13413 HHH-13171
原因在于 5.4.0.CR1 中引入的修复HHH-12436,因此从那时起,当未提供 @OneToOne(mappedBy ="") 时,一对一映射不起作用。
正如我从 JIRA 的讨论中了解到的那样,现在这不是一个错误。有一个错误,他们像这样修复了它。我想有一些误解, @PrimaryKeyJoinColumn
所以人们没有正确使用它。
我想这会解决问题
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@JsonProperty("status")
private UserStatus status;
推荐阅读
- xslt - 如何遍历多个 XML 输入文件并将相关的属性值复制到 HTML 输出表中的一行
- python - Python 和 OpenCV:在保持纵横比的同时将图像大小调整为正方形
- c# - 3D 体素网格视线 Bresenham 算法
- c - C - 奇怪的 fork() + execl() 行为
- xaml - 设置左上角的后退按钮与 Xamarin.Forms 中的徽标对齐
- python - Python Flask - 避免在动态表返回渲染模板上发布
- c# - ii 具有不同参数的相同方法名称时的 ASP.NET 路由问题
- java - Java:Collections.sort 不起作用属性?
- python - keras 形状而 UpSampling 不匹配
- ruby-on-rails - Rails 模型 - 更新 JSON 中的嵌套键/值