我有这个实体:
批次,麦芽,国家/地区-批次包含麦芽(@ManyToMany),麦芽来自某个国家/地区(@ManyToOne)。 关系如下:
批次:
@JoinTable(name="batch_malt",
joinColumns = @JoinColumn(name="batch_id"),
inverseJoinColumns = @JoinColumn(name="malt_id"))
private Set<Malt> malts = new HashSet<>();
麦芽:
@NotNull
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="country_id")
private Country country;
@ManyToMany(mappedBy="malts")
private Set<Batch> batches;
国家/地区: 没有映射,因为它是Malt的@ManyToOne。
国家服务:
@Override
public void deleteById(Long countryIdToDelete) {
Set<Malt> malts = maltRepository.findByCountry_id(countryIdToDelete);
if (malts != null) {
for (Malt tempMalt : malts) {
log.debug("Deleting country from malt number: " + tempMalt.getMaltName());
tempMalt.setCountry(null);
}
maltRepository.deleteById(countryIdToDelete);
}
}
我想删除国家(我不想删除麦芽-应该改为null)
当我尝试删除国家/地区时得到
There was an unexpected error (type=Internal Server Error, status=500).
could not execute statement; SQL [n/a]; constraint ["FKM636T1NDS3GNKJC6WG8MCG21L: PUBLIC.BATCH_MALT FOREIGN KEY(MALT_ID) REFERENCES PUBLIC.MALT(ID) (2)"; SQL statement: delete from malt where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKM636T1NDS3GNKJC6WG8MCG21L: PUBLIC.BATCH_MALT FOREIGN KEY(MALT_ID) REFERENCES PUBLIC.MALT(ID) (2)"; SQL statement:
delete from malt where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.h2.jdbc.JdbcSQLException: Naruszenie więzów integralności: "FKM636T1NDS3GNKJC6WG8MCG21L: PUBLIC.BATCH_MALT FOREIGN KEY(MALT_ID) REFERENCES PUBLIC.MALT(ID) (2)"
Referential integrity constraint violation: "FKM636T1NDS3GNKJC6WG8MCG21L: PUBLIC.BATCH_MALT FOREIGN KEY(MALT_ID) REFERENCES PUBLIC.MALT(ID) (2)"; SQL statement:
delete from malt where id=? [23503-197]
我应该更改什么才能删除国家/地区?