Model :Client
@ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "client_discipline_relation", joinColumns = @JoinColumn(name = "client_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "discipline_id", referencedColumnName = "id")) private Set<ClientDiscipline> disciplines = new HashSet<>(); Model :ClientDiscipline
@ManyToMany(mappedBy = "disciplines") private Set<Client> clients; when i go to delete a clientDiscipline i do the following
clientService.removeDiscipline(disciplineService.findById(disciplineId),client.getId()); disciplineService.delete(disciplineId); remove code :
@Override @Transactional(readOnly = false) public void removeDiscipline(ClientDiscipline discipline, Long id) { Client client=findClientById(id); Set<ClientDiscipline> existingDisciplines=client.getDisciplines(); if(existingDisciplines.contains(discipline)) { existingDisciplines.remove(discipline); } client.setDisciplines(existingDisciplines); clientRepository.save(client); } delete code :
@Override @Transactional(readOnly = false) public void delete(Long id) { Preconditions.checkNotNull(id); disciplineRepository.delete(id); } the delete operation performs well.it deletes the discipline. But it throws an exception in the console log as follows
Hibernate: delete from disciplines where id=? WARN [http-bio-8080-exec-2] (org.hibernate.engine.jdbc.spi.SqlExceptionHelper) - SQL Error: 1451, SQLState: 23000 ERROR [http-bio-8080-exec-2] (org.hibernate.engine.jdbc.spi.SqlExceptionHelper) - Cannot delete or update a parent row: a foreign key constraint fails (`hcs_zabzorg`.`client_discipline_relation`, CONSTRAINT `FKtqmvkwvwlxjgi4acfgriqk2ei` FOREIGN KEY (`discipline_id`) REFERENCES `disciplines` (`id`)) ERROR [http-bio-8080-exec-2] (org.hibernate.internal.ExceptionMapperStandardImpl) - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement] the row deletes from database.though i can delete so its not a problem but why this exception occurs and how to solve anyone knows?