title: Hibernate/JPA not able to set the parent's id to child's candidate key
body: <p>Here's the code example:</p>
  
  <p>Parent:</p>
  
  <pre><code>@Table(name="PURCHASE_ORDER")
  public class PurchaseOrder implements Serializable {
  
     private static final long serialVersionUID = 2006292307678909083L;
  
     @Id
     @SequenceGenerator(name="PURCHASE_ORDER_ID_GENERATOR", sequenceName="PURCHASE_ORDER_SEQ", allocationSize = 1)
     @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PURCHASE_ORDER_ID_GENERATOR")
     @Column(name="PURCHASE_ORDER_ID")
     private BigDecimal id;
  
     // more fields... 
  
     @JsonManagedReference
     @OneToMany(fetch = FetchType.LAZY, mappedBy="purchaseOrderByDetail", cascade = CascadeType.ALL)
     private List&lt;PurchaseOrderDetail&gt; purchaseOrderDetails;
  
  }
  </code></pre>
  
  <p>Child to <code>PurchaseOrder</code> &amp; Parent to <code>PurchaseOrderCost</code>:</p>
  
  <pre><code>@Entity
  @Table(name="PURCHASE_ORDER_DETAIL")
  public class PurchaseOrderDetail implements Serializable {
  
      @Id
      @SequenceGenerator(name="PURCHASE_ORDER_DETAIL_ID_GENERATOR", sequenceName="PURCHASE_ORDER_DETAIL_SEQ", allocationSize = 1)
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PURCHASE_ORDER_DETAIL_ID_GENERATOR")
      @Column(name="PURCHASE_ORDER_DETAIL_ID")
      private BigDecimal id;
  
      @Column(name="CANCEL_DATE")
      private Date cancelDate;
  
      @JsonBackReference
      @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinColumn(name="PURCHASE_ORDER_ID")
      private PurchaseOrder purchaseOrderByDetail;
  
      @JsonManagedReference
      @OneToMany(fetch=FetchType.LAZY, mappedBy="purchaseOrderDetailByCost", cascade = CascadeType.ALL)
      private List&lt;PurchaseOrderCost&gt; purchaseOrderCosts;
  
  }
  </code></pre>
  
  <p>Child to <code>PurchaseOrderDetail</code>:</p>
  
  <pre><code>@Entity
  @Table(name="PURCHASE_ORDER_COST")
  public class PurchaseOrderCost implements Serializable {
  
      @EmbeddedId
      private PurchaseOrderCostPK id;
  
      //.. other fields..
  }
  </code></pre>
  
  <p>Composite Key <code>PurchaseOrderCostPK</code> details:</p>
  
  <pre><code>@Embeddable
  public class PurchaseOrderCostPK implements Serializable {
  
      @Column(name="COST_TYPE_ID")
      private BigDecimal costTypeId;
  
      @Column(name="PURCHASE_ORDER_DETAIL_ID")
      private BigDecimal purchaseOrderDetailId;
  
      //.. overridden equals &amp; hashCode methods implemented...
  
  }
  </code></pre>
  
  <p>Now in the client code where I am populating entity objects with values, I have done this:</p>
  
  <pre><code>PurchaseOrderCost cost = PurchaseOrderCost.builder()
                  .id(PurchaseOrderCostPK.builder()
                          .costTypeId(costTypeEntry.getLovEntryId())
                          .purchaseOrderDetailId(purchaseOrderDetailParent.getId())
                          .build())
  </code></pre>
  
  <p>When I call the save from PurchaseOrderRepository having all the children object set properly, I am getting this issue:</p>
  
  <pre><code>2020-02-04 16:14:09 [http-nio-8080-exec-2] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-01400: cannot insert NULL into ("&lt;SCHEMA&gt;"."PURCHASE_ORDER_COST"."PURCHASE_ORDER_DETAIL_ID")
  2020-02-04 16:14:10 [http-nio-8080-exec-2] ERROR o.h.i.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
  2020-02-04 16:16:37 [http-nio-8080-exec-2] ERROR c.r.e.e.EnterpriseExceptionHandler - EnterpriseExceptionHandler:  
  org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:296)
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:536)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:533)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  </code></pre>