gpt4 book ai didi

java - 玩!删除时的框架参照完整性约束

转载 作者:行者123 更新时间:2023-12-04 05:55:13 29 4
gpt4 key购买 nike

我创建了一些 Model 对象来代表一家拥有多个客户的公司,以及一个由公司和客户组合以及多个发票行组成的发票对象。我创建了以下模型对象:

@Entity
public class Company extends Model {
@OneToMany(mappedBy="company")
public Set<Client> clients;
}

@Entity
public class Client extends Model {
@ManyToOne
public Company company;
}

@Entity
public class Invoice extends Model {
@ManyToOne
public Company company;
@ManyToOne
public Client client;
@OneToMany(mappedBy="invoice", cascade=CascadeType.ALL)
public Set<InvoiceLine> invoiceLines;
}

@Entity
public class InvoiceLine extends Model {
@ManyToOne
public Invoice invoice;
}

考试:
@Test
public void testModels() {
Client client = createClient();
Company company = createCompany();
company.clients = new HashSet<Client>();
company.clients.add(client);
company.save();

Invoice invoice = createInvoice(client, company);
InvoiceLine invoiceLine1 = createInvoiceLine(invoice);
InvoiceLine invoiceLine2 = createInvoiceLine(invoice);
Set<InvoiceLine> invoiceLines = new HashSet<InvoiceLine>();
invoiceLines.add(invoiceLine1);
invoiceLines.add(invoiceLine2);
invoice.invoiceLines = invoiceLines;
invoice.save();

Company retrievedCompany = Company.find("byName", company.name).first();
assertNotNull(retrievedCompany);
assertEquals(1, retrievedCompany.clients.size());
assertEquals(2, InvoiceLine.count());

assertEquals(1, Invoice.deleteAll());
assertNull(Invoice.all());
assertNull(InvoiceLine.all());

}

运行创建带有两个发票行的发票的测试并尝试删除此发票时,出现以下错误:

org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK3004B0A1F4110EF6: PUBLIC.INVOICELINE FOREIGN KEY(INVOICE_ID) REFERENCES >PUBLIC.INVOICE(ID)"; SQL statement: delete from Invoice [23003-149]



我究竟做错了什么?

最佳答案

你有没有用 invoice.delete() 试过这个?问题是deleteAll()不级联删除操作。
deleteAll()使用 javax.persistence.Query在内部,而 delete()使用 EntityManagerremove()方法。 JPA 中的级联由 JPA 处理而不是由数据库处理,并且 JPA 不像 deleteAll() 执行的那样级联批量删除。 .
Check this link for more info on bulk delete/update .

另外:将 InvoiceLine 实体添加到 Invoice如果您已经设置 Invoice,则是多余的作为 createInvoiceLine() 中的父级.就做invoice.refresh()在执行断言之前。

也许以下单元测试可以解决问题。Parent1就像你的 Invoice .虽然 Child1就像你的 InvoiceLine .

import java.util.*;
import javax.persistence.*;
import org.junit.*;
import play.test.*;
import models.*;

public class Parent1Test extends UnitTest {

public Parent1 p;
public Child1 c1;
public Child1 c2;
public Child1 c3;

@Before
public void setUp() {
Fixtures.deleteAllModels();

p = new Parent1();
c1 = new Child1();
c2 = new Child1();
c3 = new Child1();
}

public void byAddingParentToChilds() {
c1.parent = p;
c2.parent = p;
c3.parent = p;

c1.save();
c2.save();
c3.save();
p.refresh();
}

@Test
public void testByAddingParentToChilds() {
byAddingParentToChilds();
assertEquals(p.id, c1.parent.id);
assertEquals(3, Child1.count());
}

public void byAddingChildsToParent() {
p.childs = new ArrayList<Child1>();
p.childs.add(c1);
p.childs.add(c2);
p.childs.add(c3);
p.save();
}

@Test
public void testByAddingChildsToParent() {
// By adding childs
byAddingChildsToParent();

c1.refresh();
assertEquals(3, Child1.count());
// This will be null, because you added the childs to the
// parent while the childs are the owning side of the
// relation.
assertNull(c1.parent);
}

@Test
public void testDeletingAfterAddingParentToChilds() {
byAddingParentToChilds();
p.delete();
assertEquals(0, Parent1.count());
assertEquals(0, Child1.count());
}

@Test
public void testDeletingAfterAddingChildsToParent() {
byAddingChildsToParent();
p.delete();
assertEquals(0, Parent1.count());
assertEquals(0, Child1.count());
}

@Test(expected=PersistenceException.class)
public void testDeleteAllAfterAddingParentToChilds() {
byAddingParentToChilds();
// The cascading doesn't work for deleteAll() so this line
// will throw an exception because the child elements still
// reference the parent.
assertEquals(1, Parent1.deleteAll());
}

@Test
public void testDeleteAllAfterAddingChildsToParent() {
byAddingChildsToParent();
assertEquals(1, Parent1.deleteAll());
assertEquals(0, Parent1.count());
// Again the cascading doesn't work for deleteAll()
assertEquals(3, Child1.count());
}

}

关于java - 玩!删除时的框架参照完整性约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9585565/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com