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: 1 to M but don't want a 'Many' Property in the '1' class
PostPosted: Fri Nov 02, 2007 4:32 pm 
Regular
Regular

Joined: Thu Nov 30, 2006 10:48 am
Posts: 59
I have 2 classes, NetworkInterface and InterfaceGroupType; the NetworkInterface class contains an instance of the NetworkInterfaceType class. The database tables for each are tblNetworkInterface and tblInterfaceGroupType, respectively. tblNetworkInterface has a foreign key, InterfaceGroupTypeID, to tblInterfaceGroupType. All tables have an integer primary key, ID.

Although it's technically a many to one (NetworkInterface is associated with only one InterfaceGroupType; InterfaceGroupTypecan be associated with many NetworkInterfaces), it doesn't make sense for us to include a collection of NetworkInterface objects in the InterfaceGroupTypeclass.

I'm very new at nHibernate and was wondering how I can best do this. I assume a formula can work but is there a better way?

Hibernate version: It's 1.2.0.GA merged with the NH-466 patch (I built a custom version because I needed the 'join' tag)

Name and version of the database you are using: Microsoft SQL Server 2005; Version: 9.00.1406.00; Product Level: RTM; Edition: Developer Edition


Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">

<class name="Netops.CHNOPS.Domain.NetworkInterface.NetworkIntrface, Netops.CHNOPS.Domain" table="dbo.tblNetworkInterface">

<id type="Int32" column="ID" name="ID" unsaved-value="null">
<generator class="identity"></generator>
</id>

<property name="GroupType" formula="(select IGT.TypeName from ltblInterfaceGroupType IGT where IGT.ID = ???.InterfaceGroupTypeID)"/>

</class>

</hibernate-mapping>

Thanks,
Bill

_________________
metazone


Top
 Profile  
 
 Post subject: Re: 1 to M but don't want a 'Many' Property in the '1' class
PostPosted: Fri Nov 02, 2007 4:56 pm 
Newbie

Joined: Fri Nov 02, 2007 11:08 am
Posts: 6
I think you would still have a <many-to-one> element in your mapping for NetworkInterface. For instance:

Code:
<many-to-one name="GroupType" class="NetworkInterfaceType" column="ID" />


What you wouldn't have is a <one-to-many> element in your mapping for InterfaceGroupType, because you don't want the collection of NetworkInterfaces.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 03, 2007 10:26 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
rmuti is exactly right. Remember that the terms many-to-one, one-to-many, many-to-many are not NHibernate specific terms. They are used commonly in database modeling. The terms are actually the way they are to help you map your object to the appropriate database schema. You do not have to create a collection of items every time you create a many-to-one association.

There is also a one-to-one association but this is NOT what you want. In that case you could never have two network interfaces with the same type.


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.