我有以下服务:
@Service
@Transactional
public class blablaService{
....
@Override
public void deleteSystemGroups(Set<Long> ids) {
List<Terminal> terminals = terminalDao.findAll();
Set<SystemGroup> systemGroupsToDelete = systemGroupDao.getByIds(ids);
for (Terminal terminal: terminals) {
terminal.getTerminalSystemGroupsSet().removeAll(systemGroupsToDelete); // important
terminalDao.updateTerminal(terminal);
}
systemGroupDao.delete(ids); // important
}
}
以及dao:
public boolean updateTerminal(Terminal terminal) {
Session session = sessionFactory.getCurrentSession();
session.update(terminal);
session.flush(); // ATTENTION HERE!!!
return true;
}
现在它正在工作但是如果删除行
session.flush();
我看到以下错误:
org.postgresql.util.PSQLException: ERROR: update or delete on table "system_group" violates foreign key constraint "fk2593ed524a9b4be6" on table "terminal_system_group"| Detail: Key (group_id)=(2) is still referenced from table "terminal_system_group".
我理解问题的原因,但我不确定我的修复是否正常。
你能建议更好的解决方案吗?