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.  [ 5 posts ] 
Author Message
 Post subject: many to one relationship
PostPosted: Thu Feb 26, 2009 7:17 pm 
Newbie

Joined: Wed Feb 25, 2009 5:09 pm
Posts: 8
I have a User table and a Country table.

My Country table is filled with all the countries around the world.

Country(id,country_name)

When i register a user with my c#.net website i want them to choose a country and i want that country to be stored in my user table(fk_country_id).

In my User mapping file i have a many to one relationship between the user table and the country table.

In my User class I have a getter and setter for my country object.

However, i'm not sure how i can save the selected country value in my user table.

I thought this would work(but it doesn't):

User newUser = new User();
newUser.Country.Id= System.Convert.ToInt32(drpdwnCountry.SelectedValue);

where Country is my object being returned in my User class.

Can someone please help me with this?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 8:35 pm 
Newbie

Joined: Thu Feb 26, 2009 8:21 pm
Posts: 4
Assuming that your mapping files are correct, the following code should be work:
Code:
// Load country from database
Country selCountry = countryRepository.GetById( drpdwnCountry.SelectedValue );
// Initialize user object
User newUser = new User(){ Name = "Nathan", Country = selCountry };
// Persist new user
userRepository.Add( newUser );


Perhaps this article will help you: http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 11:29 am 
Newbie

Joined: Wed Feb 25, 2009 5:09 pm
Posts: 8
thanks i'll give it a try


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 5:59 am 
Newbie

Joined: Mon Mar 02, 2009 5:46 am
Posts: 5
Create a DaoFactory for Country like CountryDao

and try like this

User newUser = new User();
newUser.Country= CountryDao.getbyId(System.Convert.ToInt32(drpdwnCountry.SelectedValue),false);


try Mapping File like this

<many-to-one name="StatusId" column="statusId" class="HospiceCare.Core.Domain.Country, HospiceCare.Core" />

Please change to your Style..

Hope this will help you

Regards
Feroz.K


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 6:00 am 
Newbie

Joined: Mon Mar 02, 2009 5:46 am
Posts: 5
Create a DaoFactory for Country like CountryDao

and try like this

User newUser = new User();
newUser.Country= CountryDao.getbyId(System.Convert.ToInt32(drpdwnCountry.SelectedValue),false);


try Mapping File like this

<many-to-one name="StatusId" column="statusId" class="HospiceCare.Core.Domain.Country, HospiceCare.Core" />

Please change to your Style..

Hope this will help you

Regards
Feroz.K


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.