-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping Many-to-many Associations
PostPosted: Mon Apr 03, 2006 8:17 am 
Newbie

Joined: Mon Apr 03, 2006 8:12 am
Posts: 2
My problem is like this:

(1) I have a Person table, an Address table and a PersonToAddress mapping table that contains FKs to both the Person and Address table, but also other information (which is nullable)
(2) The very fact that nullable columns exist in this mapping table, I cannot use the built-in features of many-to-many associations in NHibernate (for example using the <composite-element>)
(3) When I saw the Hibernate documentation, it said that the best way to model problems like these is to use an Association Class and use two one-to-many associations for both ends.

However, I'm not able to get this to work. Can someone send me an example of implementing this association class?

Thanks,
SriSamp


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 8:31 am 
Beginner
Beginner

Joined: Tue Mar 28, 2006 3:44 am
Posts: 22
Hi SriSamp,

I had the same "problem" a couple of days ago. Here is one of my test-mappings. It's not finetuned, but you can get a feeling how it should work. :)

Some other stuff is included in the mapping files. E. g. a parent-child mapping in table Hierarchy. Just take a look. :)

ExternalItem
Code:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="ConsoleApplication1.ExternalItem, ConsoleApplication1" table="ExternalItem">
    <id name="Id" type="int" column="ID" access="field.pascalcase-m-underscore">
      <generator class="identity"/>
    </id>
    <property name="Buid" column="BUID" type="String"/>
    <property name="ImportFlag" column="ImportFlag" type="String"/>
    <property name="Status" column="Status" type="String"/>
    <bag name="RelExternalItemHierarchies" lazy="true" access="field.pascalcase-m-underscore">
      <key column="ItemID" />
      <one-to-many class="ConsoleApplication1.RelExternalItemHierarchy, ConsoleApplication1" />
    </bag>
    <bag name="RelExternalItemProperties" lazy="true" inverse="true" access="field.pascalcase-m-underscore">
      <key column="ItemID" />
      <one-to-many class="ConsoleApplication1.RelExternalItemProperty, ConsoleApplication1" />
    </bag>
  </class>
</hibernate-mapping>


Linktable RelExternalItemHierarchry
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="ConsoleApplication1.RelExternalItemHierarchy, ConsoleApplication1" table="RelExternalItemHierarchy">
    <id name="Id" type="Decimal" column="ID" unsaved-value="0" access="field.pascalcase-m-underscore">
      <generator class="native"/>
    </id>

    <many-to-one name="Item"
                 class="ConsoleApplication1.ExternalItem, ConsoleApplication1"
                 column="ItemID"
                 cascade="all"/>
    <many-to-one name="Hierarchy"
                 class="ConsoleApplication1.Hierarchy, ConsoleApplication1"
                 column="HierarchyID"
                 cascade="all"/>

    <property name="Buid" column="BUID" type="String"/>
    <property name="Status" column="Status" type="String"/>
    <property name="Modified" column="Modified" type="DateTime"/>
  </class>
</hibernate-mapping>


Hierarchy
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="ConsoleApplication1.Hierarchy, ConsoleApplication1" table="Hierarchy">
    <id name="Id" type="Decimal" column="ID" unsaved-value="0" access="field.pascalcase-m-underscore">
      <generator class="native"/>
    </id>
    <many-to-one name="Parent" column="ParentID" class="ConsoleApplication1.Hierarchy, ConsoleApplication1"/>
    <bag name="Hierarchies" lazy="true">
      <key column="ParentID"/>
      <one-to-many class="ConsoleApplication1.Hierarchy, ConsoleApplication1"/>
    </bag>
    <property name="Buid" column="BUID" type="String"/>
    <property name="HierarchyName" column="HierarchyName" type="String"/>
    <property name="Status" column="Status" type="String"/>
    <property name="Modified" column="Modified" type="DateTime"/>
    <bag name="RelExternalItemHierarchies" inverse="true" lazy="true">
      <key column="HierarchyID"/>
      <one-to-many class="ConsoleApplication1.RelExternalItemHierarchy, ConsoleApplication1"/>
    </bag>
  </class>
</hibernate-mapping>



Through the lazy option I'm able to access the Hierarchy/ies of an ExternalItem by accessing the list of RelExternalItemHierarchy and through each RelExternalItemHierarchy object I have access to the corresponding Hierarchy object.
I hope you could follow this explanaition. ;) In Visual Studio you can follow the described "path" if you set a breakpoint. Now you're able to view the attributes of e. g. ExternalItem during runtime and of course you're able to follow the "lazy loading" path. :)

Greetings,
Chavez


Top
 Profile  
 
 Post subject: Mapping Many-to-many Associations
PostPosted: Tue Apr 04, 2006 5:45 am 
Newbie

Joined: Mon Apr 03, 2006 8:12 am
Posts: 2
Thanks a lot. This really cleared by questions! I was trying something related to this yesterday but after looking at your example, things became clearer.

I had to make a few changes to the mapping file for my situation and I will blog about my full solution ASAP.

Thanks,
SriSamp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 7:05 am 
Beginner
Beginner

Joined: Tue Mar 28, 2006 3:44 am
Posts: 22
Hi SriSamp,

I'm glad that I could help you. :)
As I pointed out my example is full of possibilties for nice improvements. I'm constantly improving it. :)
So it'll be interesting to see what's your solution.

Greetings,
Chavez


Top
 Profile  
 
 Post subject: Re: Mapping Many-to-many Associations
PostPosted: Sat Apr 08, 2006 5:38 am 
Newbie

Joined: Sun Oct 16, 2005 4:54 am
Posts: 7
srisamp wrote:
I had to make a few changes to the mapping file for my situation and I will blog about my full solution ASAP.


I highly appreciate if you would share your solution.


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