title: Hibernate onetomany mapping
body: <p>I try to map a relation for the first time. I have found a lot of ways to go. </p>
  
  <p>There are two classes: 
  - Project (Parent)
  - Application (Child)</p>
  
  <p>So one project can have several applications, but one application belongs to just one project..</p>
  
  <p>I constructed them as follows:</p>
  
  <p><strong>Project.java</strong></p>
  
  <pre><code> import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.OneToMany;
  
      @Entity
      public class Project {
  
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private int id;
          @OneToMany(cascade = CascadeType.ALL)
          @JoinColumn(name = "project_id", nullable=false)
          @org.hibernate.annotations.IndexColumn(name = "project_index")
          List&lt;Application&gt; applications = new ArrayList&lt;Application&gt;();
  
          [get and set methods...]
          }
  </code></pre>
  
  <p><strong>Application.java</strong>    </p>
  
  <pre><code>import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
  
      @Entity
      public class Application {
  
           @Id
           @GeneratedValue(strategy = GenerationType.IDENTITY)
           private int id;
  
              @ManyToOne
              @JoinColumn(name = "project_id", updatable = false, insertable = false, nullable=false)
              private Project project;
  
          [get and set methods...]
       }
  </code></pre>
  
  <p>...but, unfortunately I got an exception I can not deal with:</p>
  
  <pre><code>javax.servlet.ServletException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
   org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
   org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
   org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
   org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
   org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:343)
   org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
   org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:83)
  
  root cause
  
  javax.faces.el.EvaluationException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
   com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
   javax.faces.component.UICommand.broadcast(UICommand.java:311)
   javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
   javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
   com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
   org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
   org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
   org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
   org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
   org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:343)
   org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
   org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:83)
  
  root cause
  
  org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
   org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
   org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
   org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
   org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
  </code></pre>
  
  <p>Where is my mistake? I mean there is nothing null. Only on project side</p>
  
  <hr>
  
  <p>When I remove @JoinColumn on owning side an exception occures:</p>
  
  <pre><code>java.sql.SQLException: Field 'project_id' doesn't have a default value
  </code></pre>
  
  <hr>
  
  <p>Thank you Pascal</p>