[英]Referential integrity constraint violation when deleting an object with play-framework 2.2.x
I've designed my database such that I can access a list of purchases (for history) as well as a simple foreign key link to the latest purchase. 我已经设计好数据库,以便可以访问购买清单(用于历史记录)以及指向最新购买的简单外键链接。 Everything (basic crud operations) work except for when I try to delete a customer.
除尝试删除客户的操作外,其他所有操作(基本操作)都起作用。 I've created a small sample project to exemplify the issue I'm having, which I've modeled after the demo ebean project .
我创建了一个小样本项目来举例说明我所遇到的问题,该问题是我在演示ebean项目之后建模的。
The complete code is posted on github . 完整的代码发布在github上 。
The referenced unit test (and any delete) throws the following exception: 引用的单元测试(以及任何删除)将引发以下异常:
Referential integrity constraint violation: "FK_CUSTOMER_LATESTPURCHASE_1: PUBLIC.CUSTOMER FOREIGN KEY(LATEST_PURCHASE_ID) REFERENCES PUBLIC.PURCHASE(ID) (2)"; 参照完整性约束冲突:“ FK_CUSTOMER_LATESTPURCHASE_1:PUBLIC.CUSTOMER外部密钥(LATEST_PURCHASE_ID)参考PUBLIC.PURCHASE(ID)(2)”; SQL statement: delete from purchase where customer_id = ?
SQL语句:从customer_id =?的购买中删除
Full Stack Trace here 完整堆栈跟踪在这里
package models;
@Entity
public class Customer extends Model {
// Modeled after https://github.com/ebean-orm-demo/demo-order/blob/master/src/main/java/app/data/Customer.java#L48-L49
@Id
private Long id;
private String name;
private boolean valid;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private List<Purchase> purchases;
@ManyToOne
private Purchase latestPurchase;
public void addOrder(Purchase purchase) {
this.purchases.add(purchase);
this.setLatestPurchase(purchase);
purchase.setCustomer(this);
purchase.save();
}
.. standard getters and setters ..
}
package models; 包装型号;
@Entity
public class Purchase extends Model {
@Id
private Long id;
private String name;
@ManyToOne
@JoinColumn(name="customer_id")
private Customer customer;
.. standard getters and setters ..
}
@Test
public void deleteCustomerCascades() {
running(fakeApplication(), new Runnable() {
public void run() {
Customer randy = new Customer("Randy Marsh");
randy.save();
// Let's add some purchases for randy
randy.addOrder(new Purchase("PS4"));
randy.addOrder(new Purchase("XBOX One Bundulru"));
//persist
randy.update();
Customer retrieved = Customer.find.byId(randy.getId());
assertThat(retrieved.getLatestPurchase().getName()).isEqualTo("XBOX One Bundulru");
assertNotNull(retrieved.getId());
assertEquals(retrieved.getLatestPurchase().getName(),"XBOX One Bundulru");
// There are 2 total purchases
assertEquals(Purchase.find.all().size(), 2);
// And Randy has two.
assertEquals(retrieved.getPurchases().size(),2);
// Randy is the owner of both purchases
assertEquals(randy.getLatestPurchase().getCustomer(),randy);
for (Purchase purchase : Purchase.find.all()){
assertEquals(purchase.getCustomer().getName(),"Randy Marsh");
}
// Let's delete the customer, and his purchases with it
retrieved.delete();
retrieved.update();
assertThat(Purchase.find.all().size()).isEqualTo(0);
}
});
}
You forgot to add the orphanRemoval=true
to your OneToMany relationship. 您忘记将
orphanRemoval=true
添加到OneToMany关系中。 Besides, I think you will get another exception when you update retrieved
after retrieved.update()
. 此外,我认为在
retrieved.update()
之后更新retrieved
到的内容时,您还会得到另一个异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.