-->
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: How to map this relationship
PostPosted: Thu Apr 13, 2006 5:26 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
I am in the process of converting an application which uses ADO.NET to NHibernate. We have the following relationships:
Property (contains a collection of floors)
Floor (contains a collection of rooms)
Room (contains a collection of components)
Component

This represents a series of simple one-to-many relationships. A Component though can be linked to a Floor or Property rather than a Room so the relationship is actually:
Property (collection of floors, collection of components)
Floor (collection of rooms, collection of components)
Room (contains a collection of components)
Component

This is implemented in the database by the Component table having nullable columns to store PropertyId, FloorId, RoomId i.e.
A component belonging to a property will have a PropertyId with FloorId and RoomId being null
A component belonging to a floor will have a PropertyId and FloorId with RoomId being null
A standard component will have a PropertyId, FloorId and RoomId

The existing application can create objects using SQL and this logic so the collections are correctly populated.

Can someone point me in the correct direction to implement this using NHibernate?


Top
 Profile  
 
 Post subject: Re: How to map this relationship
PostPosted: Tue Apr 18, 2006 1:11 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
In a Property object (for example) would you want the components collection to contain all the components in its Floors and Rooms? Likewise, would Floor.Components contain all the components in its Rooms?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 4:21 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
Just thought I would share the solution I used in the end.

I used a discriminator column on the BuildingComponent table, and then had a class for BuildingComponent and inherited from this for RoomBuildingComponent, FloorBuildingComponent and PropertyBuildingComponent.

The mapping file to achieve this is:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
    namespace="xxx" assembly="xxx">

    <class name="BuildingComponent" table="BuildingComponent" discriminator-value="0">

        <id name="Id" unsaved-value="-9999">
            <column name="BctId" sql-type="Int32" not-null="true"/>
            <generator class="increment" />
        </id>
          
        <discriminator column="BctType" type="int"/>
           
   <property name="Name" column="BctName" />
   Other properties ..
          
          <subclass name="PropertyBuildingComponent" discriminator-value="1">
      <many-to-one name="Property" class="Property" column="PrpId" />
   </subclass>
      
   <subclass name="FloorBuildingComponent" discriminator-value="2">
      <many-to-one name="Property" class="Property" column="PrpId" />
      <many-to-one name="Floor" class="Floor" column="FlrId" />
   </subclass>
      
   <subclass name="RoomBuildingComponent" discriminator-value="3">
      <many-to-one name="Property" class="Property" column="PrpId" />
      <many-to-one name="Floor" class="Floor" column="FlrId" />
      <many-to-one name="Room" class="Room" column="RomId" />
   </subclass>      
   
    </class>

</hibernate-mapping>


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.