I have 2 classes which represents a candidature and the document associated with this candidature.
Below are the hbm files.
Code:
<class name="CandidatureDocument, Candidatures.BLL" table="CandidatureDocument">
<id name="IdDocument" type="Int32" column="IdDocument" >
<generator class="identity" />
</id>
<property name="NomFichier" column="NomFichier" type="String" />
<many-to-one name="Candidature" column="IdCandidature" not-null="true" class="Candidature, Candidatures.BLL" />
</class>
<class name="Candidature, Candidatures.BLL" table="Candidature">
<id name="IdCandidature" type="Int32" column="IdCandidature">
<generator class="identity" />
</id>
<bag name="Documents" inverse="true" >
<key column="IdCandidature" />
<one-to-many class="CandidatureDocument, Candidatures.BLL" />
</bag>
</class>
When the following code is executed, the Candidature is created but not the Document and I can't understand why.
Code:
int idCandidature = 226;
Candidature parent = (Candidature)session.Load(typeof(Candidature), idCandidature );
CandidatureDocument child = new CandidatureDocument();
p.Documents.Add(child);
child.Candidature = parent;
session.Save(parent);
To resolve the matter, I must add the document then update the candidature. Must I do like this or is there another way ?
Code:
CandidatureDocument child = new CandidatureDocument();
session.Save(child);
int idCandidature = 226;
Candidature parent = (Candidature)session.Load(typeof(Candidature), idCandidature );
p.Documents.Add(child);
child.Candidature = parent;
session.Save(parent);
Please, help me...