TanvirChowdhury
TanvirChowdhury

Reputation: 2445

problem faced with many to many relation delete operation

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?

Upvotes: 0

Views: 67

Answers (2)

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

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);
    }

Upvotes: 1

Christian Beikov
Christian Beikov

Reputation: 16400

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

Upvotes: 0

Related Questions