多对多关系删除操作面临的问题
problem faced with many to many relation delete operation
模特:客户
@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<>();
模型:ClientDiscipline
@ManyToMany(mappedBy = "disciplines")
private Set<Client> clients;
当我删除一个 clientDiscipline 时,我会执行以下操作
clientService.removeDiscipline(disciplineService.findById(disciplineId),client.getId());
disciplineService.delete(disciplineId);
删除代码:
@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);
}
删除代码:
@Override
@Transactional(readOnly = false)
public void delete(Long id) {
Preconditions.checkNotNull(id);
disciplineRepository.delete(id);
}
删除操作执行well.it删除学科。但是在控制台日志中抛出如下异常
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]
该行从 database.though 中删除,我可以删除,所以这不是问题,但为什么会出现此异常以及如何解决知道的人吗?
尝试使用 clientRepository.saveAndFlush(client)
来刷新对联接 table 所做的更改。
手动清除 many to 关系解决了这个问题。由于我不使用任何 CASCADE 类型或孤立删除,因此必须手动清除多对多字段/
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);
}
模特:客户
@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<>();
模型:ClientDiscipline
@ManyToMany(mappedBy = "disciplines")
private Set<Client> clients;
当我删除一个 clientDiscipline 时,我会执行以下操作
clientService.removeDiscipline(disciplineService.findById(disciplineId),client.getId());
disciplineService.delete(disciplineId);
删除代码:
@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);
}
删除代码:
@Override
@Transactional(readOnly = false)
public void delete(Long id) {
Preconditions.checkNotNull(id);
disciplineRepository.delete(id);
}
删除操作执行well.it删除学科。但是在控制台日志中抛出如下异常
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]
该行从 database.though 中删除,我可以删除,所以这不是问题,但为什么会出现此异常以及如何解决知道的人吗?
尝试使用 clientRepository.saveAndFlush(client)
来刷新对联接 table 所做的更改。
手动清除 many to 关系解决了这个问题。由于我不使用任何 CASCADE 类型或孤立删除,因此必须手动清除多对多字段/
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);
}