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: Help on mapping a many-to-may relationship with 3 tables
PostPosted: Sat Aug 05, 2006 3:11 pm 
Newbie

Joined: Sat Aug 05, 2006 3:01 pm
Posts: 4
I got the following problem;

I have 3 tables:

TariffGroups, RateCards and RateCardPool.

Basically a TariffGroup is a set of RateCards. It can have 1 or more RateCard.
RateCards can belong to 1 or more TariffGroup, thats why i created a third table called RateCardPool.

RateCardPool has 2 fields, TariffGroupID and RateCardID, this are primary keys of TariffGroups and RateCards respectivelly, so as you figure out it makes a composite key.

Thanks to RateCardPool i can associate more tahn 1 RateCard to More than 1 TariffGroup.

Now, I have 3 clases:

TariffGroup, RateCard, and RateCardPool

I need some help making the mappings to make this work with NHibernate.

Can you please try to help me out!

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 05, 2006 6:13 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I had a similar problem. You end up not needing an object for the intersecting table. This table is used only in your mapping files to provide the many-to-many-relationship between the 2 objects.

Here is a sample of my mapping files...

Project

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   assembly="MyDomain" namespace="MyDomain">
   
<class name="Project" table="PROJECT">
        <id name="ProjectId" column="PROJECT_ID" type="Int64"
              unsaved-value="0">
             <generator class="native"/>
        </id>
        <bag name="ProjectClasses" table="PROJECT_CLASS_XREF" cascade="all" outer-join="true">
            <key column="PROJECT_ID" />
            <many-to-many column="PROJECT_CLASS_ID"
                    class="MyDomain.ProjectClass, MyDomain"  />
        </bag>
      ...


The following mapping file is for the ProjectClass object. This mapping file uses the inverse keyword to provide a backward reference to the Project object...

ProjectClass
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   assembly="MyDomain" namespace="MyDomain">
 
   <class name="ProjectClass" table="PROJECT_CLASS" discriminator-value="PC">

      <id name="ProjectClassId" column="PROJECT_CLASS_ID" type="Int64" unsaved-value="0">
         <generator class="native"/>
      </id>
   
    <discriminator column="CLASS_CODE" type="String" length="4"/>

    <bag name="Projects" table="PROJECT_CLASS_XREF"  inverse="true" cascade="all" outer-join="true">
      <key column="PROJECT_ID" />
      <many-to-many column="PROJECT_CLASS_ID"
                    class="MyDomain.Project, MyDomain"  />
    </bag> 

...


Hope this helps.
Greg


Top
 Profile  
 
 Post subject: Yes, it worked!
PostPosted: Sun Aug 06, 2006 3:13 am 
Newbie

Joined: Sat Aug 05, 2006 3:01 pm
Posts: 4
Thanks Greg!!!!


Top
 Profile  
 
 Post subject: Re: Yes, it worked!
PostPosted: Wed Aug 09, 2006 10:36 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
pepehammer wrote:
Thanks Greg!!!!


I wanted to follow-up and see if this in fact helped you out. Let me know..

Thanks

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 1:48 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
The most flexible solution probably is mapping all these 3 classes and the two many-to-one association between them.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 12, 2006 10:44 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
alfbolide wrote:
The most flexible solution probably is mapping all these 3 classes and the two many-to-one association between them.


I don't think I agree. Consider the following classes:

Code:
public class BankingCustomer {

        IList<Account> _Accounts;
}

public class Account {
        IList<BankingCustomer> _accountOwners;
}


This is a many-to-many as a Customer may have many accounts, and an account can be a joint-account between 2 or more Customers.

The table structure though needs the intersecting table for the associations, your domain model does not in this case.

This is the goal of an ORM tool, is to allow developers of the Domain model to design their domain objects independant of the database implementation.

What your suggesting is to map your domain objects to reflect the table structure. That is a more TableModule design pattern instead of the domain model pattern.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 13, 2006 8:53 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
gcook1@shaw.ca wrote:
alfbolide wrote:
The most flexible solution probably is mapping all these 3 classes and the two many-to-one association between them.


I don't think I agree. Consider the following classes:

Code:
public class BankingCustomer {

        IList<Account> _Accounts;
}

public class Account {
        IList<BankingCustomer> _accountOwners;
}


This is a many-to-many as a Customer may have many accounts, and an account can be a joint-account between 2 or more Customers.

The table structure though needs the intersecting table for the associations, your domain model does not in this case.

This is the goal of an ORM tool, is to allow developers of the Domain model to design their domain objects independant of the database implementation.

What your suggesting is to map your domain objects to reflect the table structure. That is a more TableModule design pattern instead of the domain model pattern.

I don't think I was suggesting a TableModule design. Actually it is also recommended by Hibernate in Action (check out the banner).
In my experience, adding an intermediate class between Account and Customer will give you the following strength:
first, you will have a class that can help manage this complex association.
secondly, in this class, you can store more information such as the time when a customer is added as an extra owner to the joint-account or the priority of the owners over the account.
This design might not be suitable for you domain but these two capabilities just give you more flexibility as I was suggesting.


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.