我有以下實體:唯一約束違反有關休眠更新到一對多集合
@Entity
public class License {
// ...
@OneToMany(mappedBy = "license", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<Service> services = new HashSet<>();
// ...
protected void setServices(Set<String> services) {
this.services.clear();
if (services != null) {
this.services.addAll(services.stream().map(s -> new Service(this, s)).collect(toSet()));
}
}
}
@Entity
@Table(uniqueConstraints = @UniqueConstraint(name = "UQ_lic_service", columnNames = { "license_id", "service" }))
public class Service {
// ...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "license_id", nullable = false)
private License license;
@Column(nullable = false, length = 255)
private String service;
protected Service(License license, String service) {
this.license = license;
this.service = service;
}
}
現在我需要更新License
,如:
License license = getSomeExistingLicenseWithServices(...);
// The existing license has two services "firstService", "secondService"
HashSet<String> services = new HashSet<>();
services.add("firstService");
license.setServices(services);
licenseRepository.save(license); // A spring-data-jpa-repository
運行,我得到以下異常:
Caused by: org.h2.jdbc.JdbcSQLException: Eindeutiger Index oder Primärschlüssel verletzt: "UQ_LIC_SERVICE_INDEX_8 ON PUBLIC.SERVICE(LICENSE_ID, SERVICE) VALUES (1, 'firstService', 1)"
Unique index or primary key violation: "UQ_LIC_SERVICE_INDEX_8 ON PUBLIC.SERVICE(LICENSE_ID, SERVICE) VALUES (1, 'firstService', 1)"; SQL statement:
insert into service (id, lock_no, license_id, service) values (null, ?, ?, ?) [23505-196]
我預計所有'老'(='孤立')Service
s將被刪除,當我撥打setServices
與新的HashSet
。我做錯了什麼? TIA!
前加入這樣的事情,我已經這樣做,在'License.setServices'。 – t777
是不一樣清除之前單獨添加 查找此http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#collections-值 –
好吧。我試過了,但是我得到了同樣的錯誤。 – t777