org.hibernate.PropertyValueException:非空属性引用空值或瞬态值:示例。 forms.voiceItem.invoice
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
....(截断)
hibernate映射:
< hibernate-mapping package = example.forms >
< class name =Invoicetable =Invoices>
< id name =idtype =long>
< generator class =native/>
< / id>
< property name =invDatetype =timestamp/>
< property name =customerIdtype =int/>
< set cascade =allinverse =truelazy =truename =itemsorder-by =id>
< key column =invoiceId/>
< / set>
< / class>
< class name =InvoiceItemtable =InvoiceItems>
< id column =idname =itemIdtype =long>
< generator class =native/>
< / id>
< property name =productIdtype =long/>
< property name =packnametype =string/>
< property name =quantitytype =int/>
< property name =pricetype =double/>
< / class>
< / hibernate-mapping>
InvoiceManager.java
class InvoiceManager {
public Long save(Invoice theInvoice)throws RemoteException {
Session session = HbmUtils.getSessionFactory()。getCurrentSession();
Transaction tx = null;
Long id = null;
尝试{
tx = session.beginTransaction();
session.persist(theInvoice);
tx.commit();
id = theInvoice.getId();
} catch(RuntimeException e){
if(tx!= null)
tx.rollback();
e.printStackTrace();
抛出新的RemoteException(发票无法保存);
} finally {
if(session.isOpen())
session.close();
}
return id;
$ / code $ / pre>
Invoice.java
public class Invoice implements java.io.Serializable {
private Long id;
私人日期invDate;
private int customerId;
private Set< InvoiceItem>项目;
public Long getId(){
return id;
}
public Date getInvDate(){
return invDate;
}
public int getCustomerId(){
return customerId;
}
public Set< InvoiceItem> getItems(){
返回项目;
}
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;
}
}
InvoiceItem.java
public class InvoiceItem implements java.io.Serializable {
private Long itemId;
私人长期产品ID;
private String packname;
private int数量;
私人双重价格;
私人发票发票;
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发票getInvoice(){
返回发票;
}
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;
}
}
解决方案 InvoiceItem
必须有一个发票
,因为 not-null =true在多对一映射中
。
所以基本的想法是你需要在代码中设置显式关系。有很多方法可以做到这一点。在你的课上,我看到一个 setItems
方法。我没有看到 addInvoiceItem
方法。设置项目时,您需要循环访问该组,并在所有项目上调用 item.setInvoice(this)
。如果你实现了一个 addItem
方法,你需要做同样的事情。或者您需要另外设置集合中每个 InvoiceItem
的发票。
Facing trouble in saving parent/child object with hibernate. Any idea would be highly appreciated.
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)
hibernate mapping:
<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>
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;
}
}
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;
}
}
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;
}
}
解决方案 Every InvoiceItem
must have an Invoice
attached to it because of the not-null="true"
in the many-to-one mapping.
So the basic idea is you need to set up that explicit relationship in code. There are many ways to do that. On your class I see a setItems
method. I do NOT see an addInvoiceItem
method. When you set items, you need to loop through the set and call item.setInvoice(this)
on all of the items. If you implement an addItem
method, you need to do the same thing. Or you need to otherwise set the Invoice of every InvoiceItem
in the collection.
这篇关于非空属性引用空值或瞬态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!