Spring boot version: 2.1.1 and 2.2.10 (actually any 2.1 version and any 2.2 version I could reproduce the issue below)

I know here is not the place to ask questions, but I did not find any reference in release notes of the problem that is happening to me. Since it is started to happen after I upgraded the Spring boot version, I thought that it must be documented somewhere, so we could know that it can happen.

I am having an error ORA-02291: integrity constraint (ADDRESS_R01) violated - parent key not found after I updated the spring boot version in my application. The error is happening when the function userRepository.save is called, which should trigger the save in users table first and after that the save in address table.

When I enable show_sql, I can see that what is happening is that address is being saved before the users, and the error above is thrown. Which does not happen in previous version.

If I downgrade the spring boot to 2.1.X, which was the version before, everything works fine.

The relationship between these two tables are represented in the classes below using @OneToOne annotation. The classes can be seen in (the classes are just for example): https://stackoverflow.com/questions/65322780/parent-key-not-found-after-upgrading-spring-boot-from-2-1-1-to-2-2-10?noredirect=1#comment115484129_65322780

I found that if I invert the @JoinColumn annotation and mappedBy attribute everything works fine in version 2.2.X. The FK is in Address table.

I did not find any reference to this problem in release notes/documentation. Can someone explain what is started to happening in version 2.2.X. I have a lot of microservices to do maintenance and I am afraid that this could be happening somewhere else.

@Builder
@AllArgsConstructor
@Entity
@Table(name = "USERS")
public class UserEntity {
  @Id
  @Column(name = "id", unique = true, nullable = false, length = 40)
  public String getId()
  {
    return this.id;
  }

  @Column(name = "FIRST_NAME", length = 75, nullable = false)
  @NotNull
  public String getFirstName()
  {
    return this.firstName;
  }

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "id", unique = true, nullable = false)
  public Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

Address class

@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString
@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @Column(
      name = "id",
      unique = true,
      nullable = false,
      length = 40)
  public String getId() {
    return id;
  }

  @OneToOne(cascade = CascadeType.ALL, mappedBy = "address", fetch = FetchType.LAZY)
  public UserEntity getUser() {
    return this.user;
  }

  public void setUser(UserEntity user) {
    this.user = user;
  }

Aditional infos to identify the problem:

  1. The tables, as it is a OneToOne releationship, has a shared id, the id from both tables are the same.
  2. The FK is in the Address table. I learnt that @JoinColumn should be in this table, but it was not and it was working fine. It is a new code that I am doing maintenance.

Comment From: wilkinsona

Spring Boot itself isn't involved in persisting entities, that's done by Hibernate. Spring Boot 2.1.x uses Hibernate 5.3.x and Spring Boot 2.2.x uses Hibernate 5.4.x so I suspect that the change in behaviour is due to a change in Hibernate 5.4.

Searching for ORA-02291 and Hibernate shows that there have been bugs in this area in the past, particularly when hibernate.order_inserts is set to true.

If you can provide a minimal sample that reproduces the problem, I would recommend raising this with the Hibernate team. Alternatively, you may want to ask on Stack Overflow so that someone with Hibernate expertise can hopefully help you.

Comment From: rafael-andrade

Spring Boot itself isn't involved in persisting entities, that's done by Hibernate. Spring Boot 2.1.x uses Hibernate 5.3.x and Spring Boot 2.2.x uses Hibernate 5.4.x so I suspect that the change in behaviour is due to a change in Hibernate 5.4.

Searching for ORA-02291 and Hibernate shows that there have been bugs in this area in the past, particularly when hibernate.order_inserts is set to true.

If you can provide a minimal sample that reproduces the problem, I would recommend raising this with the Hibernate team. Alternatively, you may want to ask on Stack Overflow so that someone with Hibernate expertise can hopefully help you.

Thanks, I will check on that.