The null-null property refers to a null value or a transition value

Risk of problems with saving parent / child in sleep mode. 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; } } 
+48
hibernate
Jun 17 '11 at 17:41
source share
6 answers

Each InvoiceItem must have an Invoice attached to it due to not-null="true" in a many-to-one mapping.

So, the main idea is that you need to configure this explicit link in the code. There are many ways to do this. In your class, I see the setItems method. I do not see the addInvoiceItem method. When you set items, you need to go through the set and call item.setInvoice(this) for all items. If you implement the addItem method, you need to do the same. Or you need to otherwise set an invoice for each InvoiceItem in the collection.

+28
Jun 17 2018-11-17T00:
source share
β€” -

for followers, this error message may also mean "you are referencing a foreign object that has not yet been saved to the database" (although it is not equal to zero there).

+57
Mar 30 '12 at 19:15
source share

Check the unsaved values ​​for your primary key / object id in hbm files. If you have automatic creation of an identifier using the hibernate framework and you set the identifier somewhere, it will throw this error. By default, the unsaved value is 0, so if you set the identifier to 0, you will see this error.

+2
Jun 08 '13 at 19:48
source share

It could be simple:

 @Column(name = "Some_Column", nullable = false) 

but when saved, the value of "Some_Column" is null, even if "Some_Column" cannot be any primary or foreign key.

+2
Jun 25 '15 at 13:13
source share

I was getting the same error, but solved it finally, in fact I did not install the Object Entity that was already saved for another object, and therefore the Object value that it received for the foreeign key was null.

0
Mar 24 '15 at 6:52
source share

Make this variable transient. Your problem will be resolved.

 @Column(name="emp_name", nullable=false, length=30) private transient String empName; 
-7
May 12 '16 at 8:24
source share



All Articles