我收到了这个不应该收到的错误。首先,我坚持与医生 1 的预约 1,并且它被保存。然后,在 Appointment4 上,我收到分离实体错误,但 doctor1 已保存=它有一个 ID 设置,它在数据库中。这是怎么回事?决议是什么? 谢谢你。
Appointment appointment1 = new Appointment("08/11/11", patient1,
payment1, doctor1);
Appointment appointment2 = new Appointment("12-11-2008", patient2,
payment2, doctor2);
Appointment appointment3 = new Appointment("13-11-2008", patient3,
payment3, doctor2);
Appointment appointment4 = new Appointment("14-11-2008", patient1,
payment4, doctor1);
appointmentRepository.save(appointment1);
// detached entity passed to persist: domain.Doctor
appointmentRepository.save(appointment4);
好吧,显然只需为父方法执行
@Transaction
就足够了。默认情况下,事务仅限于单个存储库方法,因此当它返回时,约会及其子项将变得分离。将 @Transaction 添加到父级使它们全部处于同一事务中。