-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: not-null property references a null or transient value
PostPosted: Mon Mar 24, 2008 12:17 pm 
Newbie

Joined: Mon Mar 24, 2008 11:33 am
Posts: 3
Hello,

I get the following exception, when i try to save some values in Database:




Code:

org.hibernate.PropertyValueException: not-null property references a null or transient value: model.Customerorder.customer
   at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
   at controller.CustomerorderController.processCustomerorder(CustomerorderController.java:91)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.apache.el.parser.AstValue.invoke(AstValue.java:131)
   at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
   at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
   at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:88)
   at javax.faces.event.ActionEvent.processListener(ActionEvent.java:77)
   at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:746)
   at javax.faces.component.UICommand.broadcast(UICommand.java:368)
   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
   at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
   at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
   at java.lang.Thread.run(Unknown Source)




Here is the java-Code thad should save the values into the database:


Code:
...
Session session = HibernateUtil.getSessionFactory().openSession();
       Transaction tx = session.beginTransaction();
       session.beginTransaction();
       
       
       
       Date d = new Date();

       
       custorder.setCustomerid(null);
       custorder.setOrderdate(d);
      
        session.save(custorder);
         
         tx.commit();
         session.close();
       
...




In this case there are 2 tables:

1) Customerorder:
- customerOrderId
- customerId
- orderDate

2) Customer:
- customerId
- street
- zip
- city
- country
- loginName
- pwd
- firstName
- lastName
- email
- phone



Here are the java-Calsses:

Customerorder.java



Code:

public class Customerorder {
   
   private Integer customerorderid;
   private Integer customerid;
   private Date orderdate;
   
////////////////////////////////////////////////////////
   
   private Customer customer;
   

   public Customer getCustomer() {
      return customer;
   }

   public void setCustomer(Customer customer) {
      this.customer = customer;
   }
   
   
////////////////////////////////////////////////////////   
   
   Set orderdetails = new HashSet();

   public Set getOrderdetails() {
      return orderdetails;
   }

   public void setOrderdetails(Set orderdetails) {
      this.orderdetails = orderdetails;
   }

   
   
////////////////////////////////////////////////////////
   
   
   public Integer getCustomerorderid() {
      return customerorderid;
   }

   public void setCustomerorderid(Integer customerorderid) {
      this.customerorderid = customerorderid;
   }

   public Integer getCustomerid() {
      return customerid;
   }

   public void setCustomerid(Integer customerid) {
      this.customerid = customerid;
   }

   public Date getOrderdate() {
      return orderdate;
   }

   public void setOrderdate(Date orderdate) {
      this.orderdate = orderdate;
   }
   
   
}




Customer.java

Code:

public class Customer {
   
   private Integer customerid;
   private String street;
   private String zip;
   private String city;
   private String country;
   private String loginname;
   private String pwd;
   private String firstname;
   private String lastname;
   private String email;
   private String phone;
   
   public Customer() {}
   
   
//////////////////////////////////////////////////////////////
   
   Set customerorders = new HashSet();
   
   
   public Set getCustomerorders() {
      return customerorders;
   }

   public void setCustomerorders(Set customerorders) {
      this.customerorders = customerorders;
   }

   
   
/////////////////////////////////////////////////////////////   
   
   public Integer getCustomerid() {
      return customerid;
   }

   public void setCustomerid(Integer customerid) {
      this.customerid = customerid;
   }

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getZip() {
      return zip;
   }

   public void setZip(String zip) {
      this.zip = zip;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getCountry() {
      return country;
   }

   public void setCountry(String country) {
      this.country = country;
   }

   public String getLoginname() {
      return loginname;
   }

   public void setLoginname(String loginname) {
      this.loginname = loginname;
   }

   public String getPwd() {
      return pwd;
   }

   public void setPwd(String pwd) {
      this.pwd = pwd;
   }

   public String getFirstname() {
      return firstname;
   }

   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }

   public String getLastname() {
      return lastname;
   }

   public void setLastname(String lastname) {
      this.lastname = lastname;
   }

   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public String getPhone() {
      return phone;
   }

   public void setPhone(String phone) {
      this.phone = phone;
   }
   
}




Here are my Mapping-Files:

Customerorder.hbm.xml


Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="model">
   <class
      name="Customerorder"
      table="customerorder"
   >

      <id
         name="customerorderid"
         type="integer"
         column="customerOrderId"
      >
         <generator class="increment"/>
      </id>

      <property
         name="orderdate"
         column="orderDate"
         type="date"
         not-null="true"
      />
      <many-to-one
         name="customer"
         column="customerId"
         class="Customer"
         not-null="true"
      >
      </many-to-one>
   

      <set name="orderdetails" inverse="true">
         <key column="customerOrderId"/>
         <one-to-many class="Orderdetails"/>
      </set>


   </class>   
</hibernate-mapping>




Customer.hbm.xml

Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="model">
   <class name="Customer" table="customer">
      <meta attribute="sync-DAO">false</meta>
      <id
         name="customerid"
         type="integer"
         column="customerId"
      >
         <generator class="increment"/>
      </id>

      <property
         name="firstname"
         column="FirstName"
         type="string"
         not-null="true"
         length="100"
      />
      <property
         name="lastname"
         column="LastName"
         type="string"
         not-null="true"
         length="100"
      />
      <property
         name="loginname"
         column="LoginName"
         type="string"
         not-null="true"
         length="50"
      />
      <property
         name="pwd"
         column="Pwd"
         type="string"
         not-null="true"
         length="50"
      />
      <property
         name="street"
         column="Street"
         type="string"
         not-null="true"
         length="100"
      />
      <property
         name="city"
         column="City"
         type="string"
         not-null="true"
         length="45"
      />
      <property
         name="zip"
         column="Zip"
         type="string"
         not-null="true"
         length="20"
      />
      <property
         name="country"
         column="Country"
         type="string"
         not-null="true"
         length="45"
      />
      <property
         name="email"
         column="Email"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="phone"
         column="Phone"
         type="string"
         not-null="false"
         length="20"
      />
   
      
      <set name="customerorders" inverse="true">
         <key column="customerId"/>
         <one-to-many class="Customerorder"/>
      </set>


   </class>   
</hibernate-mapping>





Do you have any Idee how can I solve this problem?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Mon Mar 24, 2008 12:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You do not need the customerId property but only the customer property and while saving a new customerorder you need to set the customer property in customerorder not customerid.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 6:35 am 
Newbie

Joined: Mon Mar 24, 2008 11:33 am
Posts: 3
Thanks farzad for your reply.

But I'am not sure if I understand you good. The only thing that I have to do, to make this thing working is to set the customer-property instead of customerid-property?


CustomerorderController.java

Code:

Customerorder custorder = new Customerorder();
   
   
   
   
   public Customerorder getCustorder() {
      return custorder;
   }




   public void setCustorder(Customerorder custorder) {
      this.custorder = custorder;
   }




...
Session session = HibernateUtil.getSessionFactory().openSession();
       Transaction tx = session.beginTransaction();
       session.beginTransaction();
       
       
       
       Date d = new Date();

       
       custorder.setCustomer(customer);  // <-- customer-property instead of customerid
       custorder.setOrderdate(d);
     
        session.save(custorder);
         
         tx.commit();
         session.close();
       
...





Can you tell me how can I set this property while saving new new customerorder ?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 9:10 am 
Newbie

Joined: Mon Mar 24, 2008 11:33 am
Posts: 3
I think I get it.

Code:
custorder.setCustomer(customer); 


while the customer is an Object of the Customer form the current session (HttpSession).

If I'am wrong, then pleas correct me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 12:42 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You got it right. You need to look up a customer object with the key you have and you need to set that object in customerorder. Hibernate will take care of foreign keys and everything.


Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.