【发布时间】:2012-03-06 14:26:57
【问题描述】:
我创建了一些模型对象来表示一家有多个客户的公司,以及一个包含公司和客户组合以及多个发票行的发票对象。我创建了以下模型对象:
@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:违反参照完整性约束: “FK3004B0A1F4110EF6:PUBLIC.INVOICELINE 外键(INVOICE_ID)参考>PUBLIC.INVOICE(ID)”; SQL 语句:从发票中删除 [23003-149]
我做错了什么?
【问题讨论】:
-
请添加测试代码查看步骤
标签: java hibernate model playframework associations