Not able to persist the parent-child with 5.4.x version while working the same with lower versions

I have one-to-one relationship entities where the id of the parent is being used as the foreign key in the child table

My parent entity is as follows:

package com.example.demo.entity;
import javax.persistence.*;

@Entity
@Table(name = "Parent")
public class SampleEntity {

    @Id
    @Column(name = "id")
    Integer Id;

    @Column(name = "name")
    String name;

    public ChildEntity getChild() {
        return child;
    }

    public void setChild(ChildEntity child) {
        this.child = child;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "id")
    private ChildEntity child;

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Child entity:

package com.example.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Child1")
public class ChildEntity {

    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Service class:

package com.example.demo.service;

import com.example.demo.entity.SampleEntity;
import com.example.demo.repository.SampleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SampleService {

  @Autowired
  SampleRepository sampleRepo;

  public List<SampleEntity> getAll() {
    return sampleRepo.getAll();
  }

  public SampleEntity createSample(SampleEntity sampleEntity) {
    return sampleRepo.save(sampleEntity);
  }
}

When saving the below content
{

“name”:“Test5”,

“child”:{

  "id":5,

  "name":"Child5"

},

“id”:5

}

getting below exception:
Hibernate: select sampleenti0_.id as id1_1_0_, sampleenti0_.name as name2_1_0_ from parent sampleenti0_ where sampleenti0_.id=?
Hibernate: select childentit0_.id as id1_0_0_, childentit0_.name as name2_0_0_ from child1 childentit0_ where childentit0_.id=?
Hibernate: insert into child1 (name, id) values (?, ?)
2021-01-21 09:56:53.545 WARN 12644 — [nio-8090-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 547, SQLState: 23000
2021-01-21 09:56:53.545 ERROR 12644 — [nio-8090-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Child1_Parent”. The conflict occurred in database “TestDB”, table “dbo.Parent”, column ‘id’.
2021-01-21 09:56:53.560 ERROR 12644 — [nio-8090-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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 root cause

com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Child1_Parent”. The conflict occurred in database “TestDB”, table “dbo.Parent”, column ‘id’.

when using the 5.4.25.Final while the same code is working fine with 5.2.x and 5.3.x versions

how the 5.4.x is different from 5.3.x in terms of persisting the entities? is there a way to change the order of insertion same as previous versions?

I think you need to use @MapsId to make this work. Can you try using the following mapping instead which is more correct?

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@MapsId
@JoinColumn(name = "id")
private ChildEntity child;

Thanks @baikov, but it doesn’t work.

In that case you will probably have to model the inverse mapping for Hibernate to see this dependency. Something like this:

public class SampleEntity {
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parent")
    private ChildEntity child;
...
}

public class ChildEntity {
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id")
    private SampleEntity parent;
...
}

Even if this works, it would be great if you could report this issue and attach a test case for reproducing the problem: https://hibernate.atlassian.net/

Thanks @beikov, it is working. i will raise it with hibernate community too. :slight_smile: