Hi there,<div><br></div><div>I&#39;m facing a weird behavior of hibernate.</div><div><br></div><div>Trying to illustrate: I have three entities, say A, B, and C. In my scheme, A has one B which has one C, which has a reference to the parent A. </div>

<div><br></div><div>This is my code:</div><div><br></div><div><div>@Entity</div><div>@Table(name = &quot;ASSOCIADOS&quot;)</div><div>public class Associado implements Serializable {</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@OneToOne( cascade=CascadeType.ALL )</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>@JoinColumn( name=&quot;idFiliacao&quot; )</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private Filiacao filiacao;</div><div><br>

</div><div>}</div><div><br></div><div><br></div><div><div>@Entity</div><div>@Table(name = &quot;FILIACOES&quot;)</div><div>public class Filiacao implements Serializable {</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@OneToOne( cascade=CascadeType.ALL )</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>@JoinColumn(name=&quot;idDependentePai&quot; )</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private Dependente pai;</div><div><br>

</div><div>}</div><div><br></div><div><div>@Entity</div><div>@Table(name = &quot;DEPENDENTES&quot;)</div><div>public class Dependente implements Serializable {</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@ManyToOne</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>@JoinColumn( name=&quot;idAssociado&quot; )</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private Associado titular;</div><div><br>

</div><div>}</div><div><br></div><div>My problem is that when I try to persist a fully configured Associado (with a Filiation that has a Dependente which, in turn, has a reference to the parent Associado) the inserts generated by Hibernate seem to be out of order. I mean, first Hibernate tries to insert the Dependente entity, which has a reference to the primary key of Associado. But, at this point, Associado was not inserted yet. So, I&#39;m getting this error:</div>

<div><br></div><div>Caused by: java.sql.BatchUpdateException: Column &#39;idAssociado&#39; cannot be null</div><div><br></div><div>And this is the SQL generated by hibernate:</div><div><br></div><div>insert into DEPENDENTES (nome, nascimento, idParentesco, idAssociado, beneficiarioPeculio, dependentePlanoSaude, id) values (&#39;Nome Qualquer&#39;, &#39;&#39;, 3, &#39;&#39;, &#39;false&#39;, &#39;false&#39;, 6900)</div>

<div><br></div><div>Right after the em.persist (I&#39;m using Hibernate JPA) I stop the thread in the debugger and I can see that All the objects got an ID assigned. However, during the commit I&#39;m getting this error.</div>

<div><br></div><div>Does anyone have any hint about this?</div><div><br></div><div>Thanks in advance!</div><div><br></div><div>Loreno</div></div></div></div></div></div></div>