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: Are one-to-many associations required to be two-way?
PostPosted: Tue Jan 06, 2009 6:21 pm 
Beginner
Beginner

Joined: Fri Jul 06, 2007 9:27 pm
Posts: 22
Are one-to-many associations required to be two-way? Let's say I have a one-to-many association from a Foo object to a set of Bar objects. My application requires only one-way navigation, from a Foo object to its Bar objects.

Does NHibernate nonetheless require me to (a) include a Foo property in my Bar class, and (b) include a <many-to-one> mapping in the Bar mapping file? Or can I simply use a <set> mapping in the Foo object (and no Foo property in my Bar object), since my business requirements specify one-way navigation?

Thanks for your help!

David Veeneman
Foresight Systems


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 8:06 pm 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
I think the answer is yes with one exception. If you allow the foreign key to Foo to be nullable in the Bar table then NHibernate will persist the many side with an insert to Bar then an update to set the foreign key.

I'd be happy to hear that this isn't the case because I don't like this answer either. The pattern I've adopted is to expose the collection as read only and use Add and Remove methods to control the collection contents. These methods would be responsible for setting Bar.Foo on the Bar being added or removed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 2:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Nothing to add to greenmtboy answers. You might want to read this

http://nhforge.org/doc/nh/en/index.html#example-parentchild

This will give you more information about that case.

_________________
--Wolfgang


Top
 Profile  
 
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.