javaspring-boothibernatespring-boot-jpajpa

Spring JPA Shared primary key causes org.hibernate.AssertionFailure: null identifier


I'm making a spring boot application with a MySQL database, and I'm trying to set something up like this example (simplified for the question) enter image description here where some optional entities can hold extra data about a user.

This is the simplified example I've setup: Person.java:

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

@Entity
public class Person {

    @Id
    @Column
    @GeneratedValue
    private Long id;

    @Column
    private String name;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person", optional = true)
    @PrimaryKeyJoinColumn
    private Car car;

    //getters, setters and constructors
    public Person() {
        super();
    }
    public Person(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Person(Long id, String name, Car car) {
        super();
        this.id = id;
        this.name = name;
        this.car = car;
    }
    public Long getId() { return id; }
    public String getName() { return name; }
    public Car getCar() { return car; }
    public void setCar(Car car) { this.car = car; }
    public void setId(Long id) { this.id = id; }
    public void setName(String name) { this.name = name; }
}

Car.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;

@Entity
public class Car {
    @Id
    @Column
    private Long id;
    
    @MapsId
    @OneToOne //NOTE A
    @JoinColumn(name ="id")
    private Person person;

    @Column
    private String brand;

    //Getters, setters and constructors
    public Car() {
        super();
    }
    public Car(Long id, Person person, String brand) { 
        super();
        this.id = id;
        this.person = person;
        this.brand = brand;
    }
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public Person getPerson() { return person; }
    public void setPerson(Person person) { this.person = person; }
    public String getBrand() { return brand; }
    public void setBrand(String brand) { this.brand = brand; }

}

And my test class:

@SpringBootTest
class BasicOneToOneTest  {

    @Autowired
    PersonRepo persons;
    @Autowired
    CarRepo cars;

    @Test
    void test() {
        Person dave = new Person(null, "Dave");
        persons.save(dave);
    
        Car car = new Car(dave.getId(), dave, "HAS_WHEELS");
        cars.save(car);

        dave = persons.findById(dave.getId()).get();
        assertEquals(true,  dave.getCar() != null );
    
        cars.delete(car);
    
        dave = persons.findById(dave.getId()).orElseGet(()->null);
        assertEquals(true,  dave!=null ); //NOTE B
    }

}

when run as is I get org.hibernate.AssertionFailure: null identifier If I change @OneToOne (at note A) to include cascade = CascadeType.ALL the null identifier goes away, but the person is also deleted. (Test at Note B)

Your help is appreciated, thanks :)


Solution

  • Found a solution I found running the test as follows works:

    @Test
    void test() {
        Person dave = new Person(null, "Dave");
        persons.save(dave);
        
        Car car = new Car(dave.getId(), dave, "HAS_WHEELS");
        dave.setCar(car); //car is set into the parent entity
        persons.save(dave); //person is saved instead of the car
        
        
        dave = persons.findById(dave.getId()).get(); 
        assertEquals(true,  dave.getCar() != null );
        
        cars.delete(car);
        
        dave = persons.findById(dave.getId()).orElseGet(()->null); //skip?
        assertEquals(true,  dave!=null );
    }
    

    Edit: car is not deleted here as expected

    Edit: changing @OneToOne to use MERGE instead of ALL seems to work

        @OneToOne(cascade = CascadeType.MERGE, mappedBy = "person", optional = true)
        @PrimaryKeyJoinColumn
        private Car car;
    

    This is the test I used after the change:

    @Test
        void test2() {
            Person dave = new Person(null, "Dave");
            persons.save(dave);
            
            Car car = new Car(dave.getId(), dave, "HAS_WHEELS");
            
            dave.setCar(car);
            persons.save(dave);
            
            dave = persons.findById(dave.getId()).get(); 
            assertEquals(true,  dave.getCar() != null , "Dave does not have car assigned");
            
            cars.delete(car);
            
            
            dave = persons.findById(dave.getId()).orElseGet(()->null);
            assertEquals(true,  dave!=null, "Dave deleted" );
            
            assertEquals( false ,  cars.existsById(car.getId()), "Car not deleted" );
        }
    

    If someone has a nicer solution I'd still be interested. but this will work for now.