JPA对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

时间:2016-07-27 16:20:57

标签: java spring hibernate jpa persistence

我很难调试为什么JPA没有联合我的@ManyToMany关系。我发现的所有答案都与丢失的级联语句有关。但我确实拥有它们仍然得到:

  

引起:org.hibernate.TransientObjectException:对象引用   未保存的瞬态实例 - 之前保存瞬态实例   flushing:com.example.iaa.model.Employee

代码基于本教程:https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/

Thread

1 个答案:

答案 0 :(得分:1)

如链接中所示,我改为

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "employee_role", joinColumns = @JoinColumn(name = "id_employee", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "id_role", referencedColumnName = "id"))
private Set<Employee> employees;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "employees")
private Set<Role> roles;

还添加了默认构造函数和eager fetch,然后就可以了。