-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Newbie:collection' items not inserted when owner is updated
PostPosted: Thu Sep 22, 2005 9:18 am 
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...


Top
  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 5:27 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
In order for nhibernate to know that it has to persist your newly created DocumentCandidature object you have to do:

1. add the attribute unsaved-value="0" or "-1" or whatever value makes sense to you in the id tag of your mappings, to tell nhibernate that it must save the object.

2. in the default constructor of your object assign this value to the id field (If it's not 0)

Regards,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 5:51 am 
Thank you very much.
That's OK for me now.


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.