Hi there,<div><br></div><div>I'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 = "ASSOCIADOS")</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="idFiliacao" )</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 = "FILIACOES")</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="idDependentePai" )</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 = "DEPENDENTES")</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="idAssociado" )</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'm getting this error:</div>
<div><br></div><div>Caused by: java.sql.BatchUpdateException: Column 'idAssociado' 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 ('Nome Qualquer', '', 3, '', 'false', 'false', 6900)</div>
<div><br></div><div>Right after the em.persist (I'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'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>