我有ManyToOne关系:在一个大厅进行多次会议。
@Entity
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class Session {
//...some params
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "hall_id")
private Hall hall;
}
@Entity
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class Hall {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
}
我想通过下一步删除会话: 遍历所有会话,清除一些数据,然后将Hall设置为null(用作外键)。
@Transactional(propagation = Propagation.REQUIRED)
public void clearSessions(List<Session> sessions) {
sessions.forEach(s -> {
s.getSessionViewers().clear();
s.getMessages().clear();
s.setHall(null);
sessionRepository.delete(s);// Error here <---
});
}
但是钢有一个错误:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败(mydb
。session
,CONSTRAINT {{ 1}}外键(FK14qoadsfdvgkbld832ddaDDk
)参考hall_id
(hall
))
我做错了什么?