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

标签 java hibernate model playframework associations

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

@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 .

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

也许下面的单元测试可以解决问题。 Parent1 就像您的发票。而 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/

相关文章:

java - 使用 Java 方法 indexof 与 substring 结合创建新字符串时出现错误输出

java - 'field list' 中的未知列客户端

java - hibernate 断言失败: null id in entry (don't flush the Session after an exception occurs)

c# - 自定义标签助手不工作

mysql - 如何在 mysql 上按 ID 分组并仅计算 5 的值?

javascript - 如何使用Java设置和读取cookie值

java - 从特定位置获取视频缩略图 - Android

mysql - 如何解决使用 hibernate 5 和 mysql 出现 HHH000346 错误?

ruby-on-rails - 我应该对 Rails 弃用警告感到害怕吗?

java - servlet 和 HTML 表单之间的连接不起作用