Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 24, 2016

not-null property references a null or transient value

not-null property references a null or transient value


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:

                                                                                                                                                                                                                      

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 items;        public Long getId() {          return id;      }      public Date getInvDate() {          return invDate;      }      public int getCustomerId() {          return customerId;      }      public Set 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 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;      }  }  

Answer by hvgotcodes for not-null property references a null or transient value


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.

Answer by rogerdpack for not-null property references a null or transient value


for followers, this error message can also mean "you have it referencing a foreign object that hasn't been saved to the DB yet" (even though it's there, and is non null).

Answer by Rohitdev for not-null property references a null or transient value


Check the unsaved values for your primary key/Object ID in your hbm files. If you have automated ID creation by hibernate framework and you are setting the ID somewhere it will throw this error.By default the unsaved value is 0, so if you set the ID to 0 you will see this error.

Answer by Pratswinz for not-null property references a null or transient value


I was getting the same error but solved it finally,actually i was not setting the Object Entity which is already saved to the other entity and hence the Object value it was getting for foreeign key was null.

Answer by Sandeep Jindal for not-null property references a null or transient value


This could be as simple as:

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

but while persisting, the value of someColumn is null, even if someColumn may not be any primary or foreign key.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.