-->
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: How to set an EmailAddress entity in a Contact?
PostPosted: Fri Sep 30, 2005 7:58 pm 
Newbie

Joined: Sat Sep 17, 2005 5:48 am
Posts: 9
I need to an email address like so:

Code:
EmailAddresse newEmailAddress = new EmailAddresse();
newEmailAddress.emailaddress = "Test@test.com";
newEmailAddress.whenchanged = System.DateTime.Now;
newEmailAddress.whencreated = System.DateTime.Now;

int newlySavedEmailAddress = (int)session.Save(newEmailAddress);


The last line returns the id of the email that was just added to the db.

I then add a contact and pass in this new id to a field in the contact table like so:

Code:
Contact newContact = new Contact();
newContact.title = "Mr";
newContact.name = "George";
newContact.surname = "Robinson";
newContact.primaryemailaddress = newlySavedEmailAddress;
newContact.birthdate = DateTime.Now;
newContact.whenchanged = DateTime.Now;
newContact.whencreated = DateTime.Now;
      
int newlySavedContact = (int)session.Save(newContact);


What I want to do is take the id of the newly added field in the contact table and pass it back into another field in the email table (one above).

Problem is i had to do this
Code:
int newlySavedEmailAddress = (int)session.Save(newEmailAddress);


to get the id, now i know that this hasn't been saved to the db yet (this is a tranasaction). But how do I add the id of the contact into a foreign key of the email when I ive already saved the email to session? Is it possible.

Help..................


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 8:13 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Code:
newContact.primaryemailaddress = newlySavedEmailAddress;


Is primaryemailaddress an EmailAddresse or an int?

Object/Relational Mappers are used to allow doing:
Code:
newContact.primaryemailaddress = newEmailAddress;

And if you enable cascading, you will not even have to save the email address before...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 9:11 pm 
Worked a treat, thanks.


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.