简体   繁体   English

休眠。 违反缓存中的外键约束

[英]Hibernate. violates foreign key constraint in cache

I have following service: 我有以下服务:

@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
      }
}

and following dao: 以及dao:

public boolean updateTerminal(Terminal terminal) {
  Session session = sessionFactory.getCurrentSession();
  session.update(terminal);
  session.flush();  // ATTENTION HERE!!!
  return true;
 }

Now it is working but if remove line 现在它正在工作,但如果删除线

session.flush();

I see following error: 我看到以下错误:

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".

I understand the cause of the problem but I am not sure that my fix is good. 我理解问题的原因,但我不确定我的修复是否正常。

Can you advise better solution? 你能建议更好的解决方案吗?

You can take a look at the different cascade-deleting options. 您可以查看不同的级联删除选项。 This page explains the basics: 本页介绍了基础知识:

Cascade operations 级联操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM