我有两个班级
Book and DeliveryRequest现在我希望当删除Book的一个对象时,所有与该书相对应的请求也会被删除。
交货申请类:
@Entity
@Table(name="Delivery_Request")
public class DeliveryRequest{
private int deliveryRequestId;
private Book book;
private User user;
@Id
@GeneratedValue
@Column(name="Delivery_Request_Id")
public int getDeliveryRequestId() {
return deliveryRequestId;
}
public void setDeliveryRequestId(int deliveryRequestId) {
this.deliveryRequestId = deliveryRequestId;
}
@ManyToOne
@JoinColumn(name="Book_Id")
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@ManyToOne
@JoinColumn(name="User_Id")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
预订课程
@Entity
public class Book {
private int bookId;
private Set<DeliveryRequest> requests = new HashSet<DeliveryRequest>(0);
@Id
@GeneratedValue
@Column(name="Book_Id")
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
@OneToMany(fetch=FetchType.EAGER , cascade=CascadeType.ALL , mappedBy="book")
public Set<DeliveryRequest> getRequests() {
return requests;
}
public void setRequests(Set<DeliveryRequest> requests) {
this.requests = requests;
}
}
可以采取哪些措施来提供这种级联效果。
答案 0 :(得分:0)
您正在寻找delete-cascade。
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE})