我正在使用 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 处测试)
感谢您的帮助,谢谢:)