【问题标题】:JpaSystemException: attempted to assign id from null one-to-one property Error with Jpa and HibernateJpaSystemException:试图从空的一对一属性分配 id Jpa 和 Hibernate 错误
【发布时间】:2020-02-29 09:59:58
【问题描述】:
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;

提前致谢!

【问题讨论】:

    标签: mysql spring hibernate jpa one-to-one


    【解决方案1】:

    解决了!

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2021-12-25
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 2021-12-18
      • 2019-11-28
      相关资源
      最近更新 更多