-->
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.  [ 7 posts ] 
Author Message
 Post subject: Single element 'collections'
PostPosted: Fri Nov 09, 2007 2:02 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
This may be a silly question, but is there a way of mapping an entity that can be null but that does NOT have a field on the parent table? For example:

parent table
id int(11)

child table
parent_id int(11)
description text

--------------------

class Parent
{
Child child; // Can be null
}

class Child
{
Parent parent; // Cannot be null
string Description;
}

This could be mapped with a set, but I can't figure out whether this kind of linkage is possible for a single element.

Thanks,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 11:55 pm 
Newbie

Joined: Mon Jun 04, 2007 8:44 pm
Posts: 8
It's still technically a many-to-one relationship. What if there where two entries in the Child table with the same parent_id? Which one would
class Parent
{
Child child;
}

refer too? I don't think you want nHibernate to determine that for you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 11:18 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Hi,

True, but in my (real-world) case the child table has a primary key on parent_id such that it can't have more than one entry per Parent. I wouldn't want to represent that in code with any kind of collection. A one-to-one member doesn't really represent it properly either since it's really one-to-zero.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 3:28 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
You can define this as a one-to-one relationship, but you may need to make the child "constrained" by the parent -- i.e. its ID will always equal the ID of the parent, and NHibernate will set it for you. This kind of mapping is commonly used when the "child" contains a non-required blob.

Unfortunately, there is currently no way in NHibernate 1.x to get it to lazy-load these one-to-one "child" entities. Java Hibernate 3.x supports it, though, and it's a major JIRA item many of us are waiting to see implemented ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 3:40 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Hi,

Yes, one-to-one is as close as I got, although it doesn't quite model it (as the row should be optional in our model), but thanks.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 9:57 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
How about one-to-many with unique = true?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 11:41 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Unique doesn't seem to be a property for one-to-many in the docs, and in any case that would require modelling the property as a collection. I'll keep having a poke around. I think in my case the <join> tag would achieve my goal, but unfortunately that's not on release yet.


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