使用hibernate保存父/子对象时遇到麻烦.任何想法都将受到高度赞赏.
org.hibernate.PropertyValueException: not-null property references a null or transient value: example.forms.InvoiceItem.invoice
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
.... (truncated)
Run Code Online (Sandbox Code Playgroud)
hibernate映射:
<hibernate-mapping package="example.forms">
<class name="Invoice" table="Invoices">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="invDate" type="timestamp" />
<property name="customerId" type="int" />
<set cascade="all" inverse="true" lazy="true" name="items" order-by="id">
<key column="invoiceId" />
<one-to-many class="InvoiceItem" />
</set>
</class>
<class name="InvoiceItem" table="InvoiceItems">
<id column="id" name="itemId" type="long">
<generator class="native" />
</id>
<property name="productId" type="long" />
<property name="packname" type="string" />
<property name="quantity" type="int" />
<property name="price" type="double" />
<many-to-one class="example.forms.Invoice" column="invoiceId" name="invoice" not-null="true" />
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
InvoiceManager.java
class InvoiceManager {
public Long save(Invoice theInvoice) throws RemoteException {
Session session = HbmUtils.getSessionFactory().getCurrentSession();
Transaction tx = null;
Long id = null;
try {
tx = session.beginTransaction();
session.persist(theInvoice);
tx.commit();
id = theInvoice.getId();
} catch (RuntimeException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
throw new RemoteException("Invoice could not be saved");
} finally {
if (session.isOpen())
session.close();
}
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
Invoice.java
public class Invoice implements java.io.Serializable {
private Long id;
private Date invDate;
private int customerId;
private Set<InvoiceItem> items;
public Long getId() {
return id;
}
public Date getInvDate() {
return invDate;
}
public int getCustomerId() {
return customerId;
}
public Set<InvoiceItem> getItems() {
return items;
}
void setId(Long id) {
this.id = id;
}
void setInvDate(Date invDate) {
this.invDate = invDate;
}
void setCustomerId(int customerId) {
this.customerId = customerId;
}
void setItems(Set<InvoiceItem> items) {
this.items = items;
}
}
Run Code Online (Sandbox Code Playgroud)
InvoiceItem.java
public class InvoiceItem implements java.io.Serializable {
private Long itemId;
private long productId;
private String packname;
private int quantity;
private double price;
private Invoice invoice;
public Long getItemId() {
return itemId;
}
public long getProductId() {
return productId;
}
public String getPackname() {
return packname;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public Invoice getInvoice() {
return invoice;
}
void setItemId(Long itemId) {
this.itemId = itemId;
}
void setProductId(long productId) {
this.productId = productId;
}
void setPackname(String packname) {
this.packname = packname;
}
void setQuantity(int quantity) {
this.quantity = quantity;
}
void setPrice(double price) {
this.price = price;
}
void setInvoice(Invoice invoice) {
this.invoice = invoice;
}
}
Run Code Online (Sandbox Code Playgroud)
rog*_*ack 75
对于关注者,此错误消息也可能意味着"您已经引用了尚未保存到数据库的外部对象"(即使它存在,并且非空).
hvg*_*des 40
由于在多对一映射中,每个InvoiceItem
必须Invoice
附加到它. not-null="true"
所以基本的想法是你需要在代码中建立明确的关系.有很多方法可以做到这一点.在你的班上我看到了一个setItems
方法.我没有看到一种addInvoiceItem
方法.设置项目时,需要循环设置并调用item.setInvoice(this)
所有项目.如果实现addItem
方法,则需要执行相同的操作.或者您需要另外设置集合中的每个发票InvoiceItem
.
这可能很简单:
@Column(name = "Some_Column", nullable = false)
Run Code Online (Sandbox Code Playgroud)
但是在持久化时,即使“ Some_Column”可能不是任何主键或外键,“ Some_Column”的值也为null。
归档时间: |
|
查看次数: |
154239 次 |
最近记录: |