简体   繁体   English

org.springframework.dao.DataIntegrityViolationException 使用 hibernate 和 spring-boot-data-jpa 将 spring-boot 从 2.1.x 更新到 2.2.x 后

[英]org.springframework.dao.DataIntegrityViolationException after update spring-boot from 2.1.x to 2.2.x using hibernate and spring-boot-data-jpa

When trying to update spring-boot from version 2.1.12 to 2.2.4 got stuck on DataIntegrityViolationException when try to insert multiple objects into MySQL using JPA.尝试将 spring-boot 从版本 2.1.12 更新到 2.2.4 时,在尝试使用 JPA 将多个对象插入 MySQL 时卡在 DataIntegrityViolationException 上。

Example object:示例对象:

@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;

}

And user 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;
    }

}

To insert object to mysql I use default jpa repository:要将对象插入 mysql,我使用默认的 jpa 存储库:

@Repository
public interface UserRepository extends JpaRepository<User, String> { 
}

With spring-boot-2.1.x userRepository.save(user) works fine but with 2.2.x it raises this exception:使用 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

With this details in log:在日志中包含此详细信息:

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)

If enable spring.jpa.show-SQL: true I found out that with spring-boot-2.2.x no insertion on User entity is happening but with old spring it is.如果启用spring.jpa.show-SQL: true我发现使用 spring-boot-2.2.x 没有插入User实体,但使用旧的 spring。

I didn't find any major change in spring-boot connecting to hibernate as well as no major change in hibernate itself after the corresponding update.我没有发现 spring-boot 连接到 hibernate 有任何重大变化,相应更新后 hibernate 本身也没有重大变化。 Is there anything updated which is not described in release notes?是否有发行说明中未描述的更新内容?

spring-boot 2.1.12 uses Hibernate 5.3.15.Final and spring-boot 2.2.4 uses Hibernate 5.4.10.Final spring-boot 2.1.12 使用 Hibernate 5.3.15.Final 和 spring-boot 2.2.4 使用 Hibernate 5.4.10.Final

Your issue seems similar to Hibernate issues HHH-13413 HHH-13171您的问题似乎类似于 Hibernate 问题HHH-13413 HHH-13171

The reason is in the fix HHH-12436 which was introduced in 5.4.0.CR1 so since then one-to-one mappings don't work when @OneToOne(mappedBy ="") didn't provided.原因是在5.4.0.CR1中引入的修复HHH-12436 中,因此当 @OneToOne(mappedBy ="") 未提供时,一对一映射不起作用。

And as I understood from disscussions in JIRA it's not a bug now.正如我从 JIRA 中的讨论中了解到的,它现在不是一个错误。 There was a bug and they fixed it like so.有一个错误,他们像这样修复了它。 I guess there is some misunderstanding with @PrimaryKeyJoinColumn so people doesn't use it right.我猜对@PrimaryKeyJoinColumn有一些误解,所以人们没有正确使用它。

I guess this will solve the problem我想这将解决问题

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@JsonProperty("status")
private UserStatus status;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java JPA Hibernate Spring @EntityListeners引发org.springframework.dao.DataIntegrityViolationException - Java JPA Hibernate Spring @EntityListeners throws org.springframework.dao.DataIntegrityViolationException Spring 启动 2.1.x 与 Kafka 1.1.x 集成 - Spring boot 2.1.x integration with Kafka 1.1.x Spring 引导 2.1.x JSP 文件找到但未呈现 - Spring Boot 2.1.x JSP file found but not rendered 如何在从 1.5.x 到 2.1.x 的 spring boot 升级期间解决“检测到自动配置周期” - How to resolve "AutoConfigure cycle detected" during spring boot upgrade from 1.5.x to 2.1.x 为什么 HATEOAS 在使用 Swagger 2.x 启动期间开始为 spring-boot 版本 &gt;= 2.2.x 创建问题? - Why HATEOAS starts creating issue for spring-boot version >= 2.2.x during startup with Swagger 2.x? Spring 启动 JPA DataIntegrityViolationException - Spring boot JPA DataIntegrityViolationException 加载spring-boot和spring-data-jpa时,Hibernate无法加载JPA 2.1 Converter - Hibernate fails to load JPA 2.1 Converter when loaded with spring-boot and spring-data-jpa org.springframework.dao.DataIntegrityViolationException误报原因? - org.springframework.dao.DataIntegrityViolationException misreporting cause? Spring Boot和Hibernate:用于org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor - Spring Boot and Hibernate: for org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor 将 Spring Boot 从 2.1.x 升级到 2.3.3 时无法获取云配置 - Cannot fetch cloud configurations when upgrading Spring Boot from 2.1.x to 2.3.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM