[英]DataIntegrityViolationException persisting one to many relation
[英]Delete on many to one relation is not persisting
我正在嘗試從多對一關系中刪除實體。 它不會拋出任何錯誤,但似乎該項目沒有被刪除,我也不知道為什么。 這些是我的課程:
客戶實體:
public class Client extends BaseEntity<Long> {
private String name;
private String email;
private LocalDate dateOfBirth;
private LocalDate dateOfRegister;
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL, fetch =
FetchType.EAGER)
private Set<Rental> rentals = new HashSet<>();
..............
}
電影實體:
public class Movie extends BaseEntity<Long>{
private String title;
private int year;
private int duration;
private String genre;
private double imdbRating;
private String trailerLink;
@OneToMany(mappedBy = "movie", cascade = CascadeType.ALL, fetch =
FetchType.EAGER)
private Set<Rental> rentals = new HashSet<>();
..................
}
租賃(鏈接)實體:
public class Rental implements Serializable {
@Id
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "movie_id")
private Movie movie;
@Id
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "client_id")
private Client client;
@Column(name = "dateRented")
private LocalDate dateRented;
@Column(name = "dateDue")
private LocalDate dateDue;
.............
}
PrimaryKey類:
public class RentalPK implements Serializable {
private Movie movie;
private Client client;
}
在控制器中,我執行以下操作:
@Override
public void deleteRental(RentalPK rentalPK) {
try {
rentalRepository.deleteById(rentalPK);
} catch (Exception e) {
throw e;
}
}
有人可以解釋為什么它不刪除嗎?
解決了! 似乎是由於Movie和Client實體中的CascadeType.ALL。 似乎CascadeType.ALL還包含CascadeType.PERSIST,它表示只能從父級中刪除鏈接實體(在我的情況下為租用)。 因此,我已通過更換固定的問題CascadeType.ALL
與CascadeType.REMOVE
。
編輯:我不知道這是否是確切的解釋,但它起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.