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: How to load linked object in this 1:1 situation?
PostPosted: Tue Nov 27, 2007 1:29 pm 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
Hello,

I have such database schema:
Driver <-- Gender2Driver --> Gender

Three tables:
Driver - driver data, pk DriverId
Gender - gender data, pk GenderId
Gender2Driver - link 1:1, pk DriverId and GenderId

How to load Driver along with Gender?

Regards,
Mindaugas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 2:19 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Is it possible to redo your schema? If it's not too late I don't see why you need your middle table.

Now, I may not know what your tables mean, but is a Gender really 1:1 with a driver, or does one gender apply to multiple drivers? If that is the case you just want to add a GenderId to your driver. If it really is 1:1 then you really want to use the same primary key value on the Gender and Driver tables and then map them as one-to-one entities in NHibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 2:37 pm 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
It was just an example with Driver and Gender, but unfortunately it is too late to redo the schema. Yeah, I know that having this link table for 1:1 is somewhat interesting, but that's how it was done..

One Driver can be linked to only one Gender. Actually I'm interested in two situations:
1) Driver Id in the link table is unique, but Gender Id in link table is not;
2) Driver Id in the link table is unique, Gender Id in link table is also unique.

I didn't quite understood how to map Gender to Driver class, that when Driver is loaded, Gender is also loaded and set to Driver.DriverGender property. Could you provide more details how to map Gender to Driver having this link table in between?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 8:38 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Ok, you can basically map this two ways. I am not sure which way is technically more valid, I believe the two should be equivalent.

Lets say you have 3 classes, Driver, DriverGender and Gender. You could map a one-to-one relationship from Driver to DriverGender where property referenced would be the DriverId of the DriverGender object. You could then do the same thing for the DriverGender to Gender.

Alternatively you could map the relationships as many-to-one with a unique="true" meaning there is only one item on the many side (in essence a one-to-one). If you don't want the Gender to be unique on the DriverGender class then you can remove the unique="true" for that relationship.

Does that make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 10:56 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
I understand, but I'm still missing something here.. So it is possible to load Driver along with Gender and access Gender directly through Driver.Gender property? How would such mapping file look like?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 11:39 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
No, I am not aware of how you could make that specific mapping work. Instead what you would do is map it as:
Code:
Driver.DriverGender.Gender

Now, that doesn't prevent you from making the property of your object Gender and then leaving the DriverGender has a private field on the object and mapping the private field.

Within the getter (and setter) of your Gender property you would simply do:
Code:
get
{
  return driverGender.Gender;
}
set
{
  driverGender.Gender = value;
}


That way to the consumer you have the mapping you wanted even though the internal structure does vary. Note that the above code does not work 100% of the time you will have to handle cases where the driverGender object is null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 1:04 pm 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
I understand the code, but as I'm new to HNibernate I don't understand how to write a mapping file for such situation, so that not only DriverGender is loaded, but also Gender is loaded ant set to DriverGender property. Could you write a sample mapping *.hbm.xml file?


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.