2017-08-24 30 views
0

,休眠拋出異常:休眠拋出不能刪除或嘗試從數據庫中刪除的項目更新父行例外

Cannot delete or update a parent row: a foreign key constraint fails (`ticketer`.`UserOrder_Ticket`, CONSTRAINT `FKiyeesx6teiqckayn6agqw5b58` FOREIGN KEY (`tickets_id`) REFERENCES `Ticket` (`id`)) 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 

我的實體:

票:

@Entity 
public class Ticket { 

@Id 
@GeneratedValue 
private Long id; 

@OneToOne(cascade = CascadeType.ALL) 
private Event event; 

private String number; 

private BigDecimal price; 
} 

UserOrder:

@Entity 
public class UserOrder { 

@Id 
@GeneratedValue 
private Long id; 

@OneToMany(cascade = CascadeType.ALL) 
@LazyCollection(LazyCollectionOption.FALSE) 
private List<Ticket> tickets; 

@OneToOne 
private User user; 

private BigDecimal price; 
private PaymentStatus paymentStatus; 
private LocalDateTime orderDate; 
} 

我的數據庫有連接實體(休眠標準)的表格。 是否有任何可能的解決方案使其工作?我正在使用MySql。

+0

什麼是ticketer? – Sam

+0

應用程序和數據庫的名稱 – egzaell

+0

我想你正試圖更新在其他表中引用爲外鍵的主鍵值。 – Nidhi257

回答

1

如果您想首先刪除UserOrder對象,則需要刪除此UserOrder對象與所有與它有關係的Ticket對象之間的關係。所以我認爲您需要使用ManyToOne批註在您的Ticket類中創建UserOrder對象。然後將該字段設置爲null,然後刪除其中存在的對象,並在UserOrder類中設置票值也爲null。