Hi there,

I'm facing a weird behavior of hibernate.

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. 

This is my code:

@Entity
@Table(name = "ASSOCIADOS")
public class Associado implements Serializable {

@OneToOne( cascade=CascadeType.ALL )
@JoinColumn( name="idFiliacao" )
private Filiacao filiacao;

}


@Entity
@Table(name = "FILIACOES")
public class Filiacao implements Serializable {

@OneToOne( cascade=CascadeType.ALL )
@JoinColumn(name="idDependentePai" )
private Dependente pai;

}

@Entity
@Table(name = "DEPENDENTES")
public class Dependente implements Serializable {

@ManyToOne
@JoinColumn( name="idAssociado" )
private Associado titular;

}

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:

Caused by: java.sql.BatchUpdateException: Column 'idAssociado' cannot be null

And this is the SQL generated by hibernate:

insert into DEPENDENTES (nome, nascimento, idParentesco, idAssociado, beneficiarioPeculio, dependentePlanoSaude, id) values ('Nome Qualquer', '', 3, '', 'false', 'false', 6900)

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.

Does anyone have any hint about this?

Thanks in advance!

Loreno