title: Hibernate exception org.hibernate.PropertyValueException: not-null property references a null or transient value
body: <p>Hello I am currently developing small app to practice hibernate. I am trying to do one-to-many relation and I am stuck.</p>
  
  <p>I have 2 Classes Question and Answer:</p>
  
  <pre><code>package com.sample;
  
  import java.util.List;
  
  public class Question {
      private int id;
      private String qname;
      private List&lt;Answer&gt; answers;
  
      public Question() {
  
      }
  
      public Question(int id, String qname, List&lt;Answer&gt; answers) {
          super();
          this.id = id;
          this.qname = qname;
          this.answers = answers;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getQname() {
          return qname;
      }
  
      public void setQname(String qname) {
          this.qname = qname;
      }
  
      public List&lt;Answer&gt; getAnswers() {
          return answers;
      }
  
      public void setAnswers(List&lt;Answer&gt; answers) {
          this.answers = answers;
      }
  
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>package com.sample;
  
  public class Answer {
      private int id;
      private String answername;
      private String postedBy;
      private Question question;
  
      public Answer() {
  
      }
  
      public Answer(int id, String answername, String postedBy, Question question) {
          super();
          this.id = id;
          this.answername = answername;
          this.postedBy = postedBy;
          this.question = question;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getAnswername() {
          return answername;
      }
  
      public void setAnswername(String answername) {
          this.answername = answername;
      }
  
      public String getPostedBy() {
          return postedBy;
      }
  
      public void setPostedBy(String postedBy) {
          this.postedBy = postedBy;
      }
  
      public Question getQuestion() {
          return question;
      }
  
      public void setQuestion(Question question) {
          this.question = question;
      }
  
  
  }
  </code></pre>
  
  <p>the configuration files are as follows:</p>
  
  <p>question.hbm.xml:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
  &lt;!DOCTYPE hibernate-mapping PUBLIC  
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.sample.Question" table="question"&gt;
          &lt;id name="id"&gt;
              &lt;generator class="increment"&gt;&lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="qname"&gt;&lt;/property&gt;
  
          &lt;bag name="answers" table="answer" inverse="true" lazy="true"
              fetch="select" cascade="all"&gt;
              &lt;key&gt;
                  &lt;column name="question_id" not-null="true"&gt;
                  &lt;/column&gt;
              &lt;/key&gt;
              &lt;one-to-many class="com.sample.Answer"&gt;&lt;/one-to-many&gt;
          &lt;/bag&gt;
  
      &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>answer.hbm.xml:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
  &lt;!DOCTYPE hibernate-mapping PUBLIC  
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.sample.Answer" table="answer"&gt;
          &lt;id name="id"&gt;
              &lt;generator class="increment"&gt;&lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="answername"&gt;&lt;/property&gt;
          &lt;property name="postedBy"&gt;&lt;/property&gt;
          &lt;many-to-one name="question" class="com.sample.Question"
              fetch="select"&gt;
              &lt;column name="question_id" not-null="true"&gt;
              &lt;/column&gt;
          &lt;/many-to-one&gt;
      &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>My main file looks like:</p>
  
  <pre><code>import java.util.ArrayList;
  
  import org.hibernate.*;
  import org.hibernate.cfg.*;
  
  public class StoreData {
      public static void main(String[] args) {
          Session session = SessionFactoryUtil.getSessionFactory()
                  .getCurrentSession();
          Transaction t = session.beginTransaction();
  
          Answer ans1 = new Answer();
          ans1.setAnswername("java is a programming language");
          ans1.setPostedBy("Ravi Malik");
  
          Answer ans2 = new Answer();
          ans2.setAnswername("java is a platform");
          ans2.setPostedBy("Sudhir Kumar");
  
          Answer ans3 = new Answer();
          ans3.setAnswername("Servlet is an Interface");
          ans3.setPostedBy("Jai Kumar");
  
          Answer ans4 = new Answer();
          ans4.setAnswername("Servlet is an API");
          ans4.setPostedBy("Arun");
  
          ArrayList&lt;Answer&gt; list1 = new ArrayList&lt;Answer&gt;();
          list1.add(ans1);
          list1.add(ans2);
  
          ArrayList&lt;Answer&gt; list2 = new ArrayList&lt;Answer&gt;();
          list2.add(ans3);
          list2.add(ans4);
  
          Question question1 = new Question();
          question1.setQname("What is Java?");
          question1.setAnswers(list1);
  
          Question question2 = new Question();
          question2.setQname("What is Servlet?");
          question2.setAnswers(list2);
  
          session.persist(question1);
          session.persist(question2);
  
          t.commit();
  //      session.close();
          System.out.println("success");
  
      }
  }
  </code></pre>
  
  <p>When I run the app I get the following error: </p>
  
  <pre><code>Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : com.sample.Answer.question
      at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106)
      at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132)
      at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:141)
      at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
      at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
      at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:168)
      at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794)
      at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314)
      at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
      at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
      at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
      at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379)
      at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319)
      at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296)
      at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
      at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
      at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:294)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
      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.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
      at com.sun.proxy.$Proxy0.persist(Unknown Source)
      at com.sample.StoreData.main(StoreData.java:46)
  </code></pre>