【发布时间】:2020-05-29 02:05:25
【问题描述】:
当尝试使用 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.x userRepository.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-starter-data-jpastarter吗? -
我一直不明白为什么当 JdbcTemplate 可用时 Spring Boot 用户会立即使用 JPA,尤其是对于像这样的简单模式。
-
@duffymo 我也是,但这是我们必须支持一段时间的遗留代码:(
-
@Anton 我的回答对您有帮助吗,因为很可能是您的情况?无论如何,请分享您的想法以解决您的问题
-
@EugeneKortov 非常感谢,它终于有所帮助。要使用它,我必须稍微改变 UserStatus 对象的结构,在 UserStatus 对象中添加对 User 对象的引用。然后你的解决方案完美地找到了。显然,我们在项目中做错了。
标签: java hibernate spring-boot jpa spring-boot-jpa