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.  [ 4 posts ] 
Author Message
 Post subject: Handling foreign keys in code
PostPosted: Wed Jul 30, 2008 2:19 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Hi,

I'm new to NHibernate and have a litte problem with my foreign keys in code.
I have mapped the foreign keys to object references in C#, which is working great. But I wonder what is the best way to set that reference if you only know the ID of the referenced object and don't have a object instance.

One way would be to load the object instance with the ID from the database. But that causes a unneeded select statement, because I don't really want to know anything about the referenced object except it's ID.

Then I tried to add a second property to my business objects which represent my foreign keys. So I had the reference to the foreign object and the foreign key as a int property. But I couldn't find a way to say NHibernate that these two properties map to the same column. And because of that inserting and updating didn't work. Have I missed something here or is that a wrong way to do it?

Right now I just create a new object myself and set it's ID like this:
Code:
child.Parent = new Parent { ID = parentID };

Is there any better way to do it?

NHibernate version: 1.2.1.GA
.NET 3.5
ASP.NET Website, one NHibernate-Session per Request


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 7:01 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try something like this:

Code:
class Child {
...
int ParentId {
    get { return parent != null ? parent.Id : parentId; set { parentId = value; }

Parent Parent { get {...}; set {...} }

}


And a mapping like this:

Code:
<property name="ParentId" column="parentid" />
<many-to-one name="Parent" column="parentid" insert="false" update="false"/>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 5:21 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Thanks for the idea, but that won't work if I used the Parent-property (to get the name of the parent for example) because then the parent-field won't be null.

Clearing the parent-field in the setter of the ParentID-property would be an option, but again only until the next property of the Parent-property is requested.

Which leads me to the next problem: If I set the ParentID-property to a new ID and then request a property of the Parent (without first updating the database), the old Parent will be returned.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 3:38 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
I found the solution for this. Instead of ISession.Get() you can use ISession.Load(). That method only returns a proxy object, so no database query is executed until someone really tries to access a property of that object.

Don't know why I didn't found this earlier :-(
It's also well documented:
Quote:
If the class is mapped with a proxy, Load() returns an object that is an uninitialized proxy and does not actually hit the database until you invoke a method of the object. This behaviour is very useful if you wish to create an association to an object without actually loading it from the database.


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