【问题标题】:Hibernate. violates foreign key constraint in cache休眠。违反缓存中的外键约束
【发布时间】: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


    【解决方案1】:

    您可以查看不同的级联删除选项。本页介绍了基础知识:

    Cascade operations

    【讨论】:

    • 我不认为你确定在阅读提到的链接后我可以实现我的愿望。
    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多