【发布时间】:2015-02-25 16:35:08
【问题描述】:
我有以下服务:
@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
}
}
和以下道:
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".
我了解问题的原因,但我不确定我的修复是否良好。
您能提出更好的解决方案吗?
【问题讨论】:
标签: java spring hibernate caching constraints