0

我正在使用 MySQL 数据库制作一个 Spring Boot 应用程序,并且我正在尝试设置类似这个示例(为问题简化)的 在此处输入图像描述 内容,其中一些可选实体可以保存有关用户的额外数据。

这是我设置的简化示例: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; }
}

汽车.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; }

}

还有我的测试课:

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

}

当按原样运行时,我得到org.hibernate.AssertionFailure: null identifier 如果我更改 @OneToOne (at note A) 以包含cascade = CascadeType.ALL 空标识符就会消失,但该人也会被删除。(在注 B 处测试)

感谢您的帮助,谢谢:)

4

1 回答 1

0

找到了一个解决方案,我发现运行测试如下:

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

编辑:这里没有按预期删除汽车

编辑:更改 @OneToOne 以使用MERGE而不是ALL似乎有效

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

这是我更改后使用的测试:

@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" );
    }

如果有人有更好的解决方案,我仍然会感兴趣。但这暂时可行。

于 2021-07-16T05:22:50.770 回答