mercredi 25 février 2015

Hibernate. violates foreign key constraint in cach

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:



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?


Aucun commentaire:

Enregistrer un commentaire