0

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?

2 Answers 2

1

manually clearing the many to relation solves the issue. As i dont use any CASCADE type or orphan removal so the many to many many field have to be cleared manually/

public class Discipline extends Person { private static final long serialVersionUID = 431537630160539107L; @ManyToOne @JoinColumn(name = "relation") private ClientRelation relation; @Column(name = "fax") private String fax; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "client_disciplines", joinColumns = @JoinColumn(name = "discipline_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "client_id", referencedColumnName = "id")) private Set<Client> clients = new HashSet<>(); @Column(name = "hasPermission") private boolean hasPermission = false; public Discipline(Company company) { super(company); } } @Override @Transactional(readOnly = false) public void delete(Long id) { Preconditions.checkNotNull(id); Discipline discipline=findById(id); discipline.getClients().clear(); disciplineRepository.delete(discipline); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try using clientRepository.saveAndFlush(client) instead to get the changes done to the join table flushed.

2 Comments

i am sorry that save and flush does not resolve the exception.@Christian Beikov
Are you using the latest version of Hibernate? If so, and you still have the problem, please create an issue in the issue tracker(hibernate.atlassian.net) with a test case(github.com/hibernate/hibernate-test-case-templates/blob/master/…) that reproduces the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.