JpaSystemException:试图从 null 一对一分配 id 属性 Jpa 和 Hibernate 错误

JpaSystemException: attempted to assign id from null one-to-one property Error with Jpa and Hibernate

Resolved [org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.example.bootapp.entity.Employee.user]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.example.bootapp.entity.Employee.user]]

用户实体

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "id")
@JsonIgnore
@Getter @Setter
private Employee employee;

员工实体

@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JsonIgnore
private User user;

提前致谢!

已解决!

user.setEmployee(null); // I added this line
        userCommandService.save(user);
        employee.setUser(user);
        employeeCommandService.save(employee);

通过将用户实体中的员工设置为空,我的错误消失了!