0
我有一個客戶和預訂分貝。每個預訂有1個客戶,並且客戶可以有很多預訂。所以我在客戶成員上註明了@ManyToOne關係。我想如果我刪除了客戶,預訂也會被刪除。但我只能以其他方式做到這一點,如果預訂是刪除,客戶被刪除,我不想這樣做。這裏是我的代碼:JPA CascadeType.REMOVE inverse
@Entity
@NamedQueries({
@NamedQuery(name = Customer.FIND_ALL, query = "SELECT c FROM Customer c ORDER BY c.name ASC"),
@NamedQuery(name = Customer.FIND_BY_EMAIL, query = "SELECT c FROM Customer c WHERE c.email = :email")
})
@XmlRootElement
@Table(name = "customer", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "Customer.findAll";
public static final String FIND_BY_EMAIL = "Customer.findByEmail";
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@NotNull
@NotEmpty
@Size(min = 1, max = 50)
@Pattern(regexp = "[A-Za-z\\s]+")
@Column(name = "name")
private String name;
@Entity
@NamedQueries({
@NamedQuery(name = Booking.FIND_ALL, query = "SELECT b FROM Booking b ORDER BY b.bookingDate ASC")
})
@XmlRootElement
@Table(name = "booking", uniqueConstraints = @UniqueConstraint(columnNames = {"customer", "booking_date"}))
public class Booking implements Serializable {
private static final long serialVersionUID = -1L;
public static final String FIND_ALL = "Booking.findAll";
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@NotNull
@ManyToOne(cascade = {CascadeType.REMOVE})
@JoinColumn(name = "customer")
private Customer customer;
@NotNull
@Future(message = "Bookings must be in future")
@Column(name = "booking_date")
@Temporal(value = TemporalType.DATE)
private Date bookingDate;
所以,如果我刪除客戶,預訂也應該刪除。有任何想法嗎?
謝謝
從ManyToOne中刪除級聯= {CascadeType.REMOVE},並將其添加到關聯的OneToMany的反面。 –
我應該把它放在哪裏,因爲客戶中沒有預訂成員。客戶中是否必須有預訂清單? –
是的。一套將是一個更好的選擇。閱讀有關雙向OneToMany關聯的hibernate文檔。 –