-->
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.  [ 4 posts ] 
Author Message
 Post subject: One-To-Many with a Join Table
PostPosted: Sun May 17, 2009 3:18 pm 
Newbie

Joined: Sun May 17, 2009 2:14 pm
Posts: 4
Greetings, all!

I'm working on a legacy database in which an individual has many accounts, and an account belongs to a single individual. There are three tables: Individual, IndividualAccount, and Account:
Code:
+------------+      +--------------------+                 
| Individual |      | IndividualAccount  |      +----------+
+------------+      +--------------------+      | Account  |
| Id (PK)    | <----| IndividualId (FK)  |      +----------+
|            |      | AccountId (FK)     |----> | Id (PK)  |
+------------+      +--------------------+      | Type     |
                                                +----------+

I'd like to have a Individual class and an Account class that look like this:
Code:
public class Individual
{
    public virtual int Id { get; set; }
    public virtual IList<Account> { get; set; }
}

public class Account
{
    public virtual int Id { get; set; }
    public virtual Individual Individual { get; set; }
    public virtual string Type { get; set; }
}

I can't figure out a way to get the mapping to do what I want. Does NHibernate support this?

The best I've been able to do is use a many-to-many relationship in the Individual mapping and a <join> combined with an many-to-one relationship in the Account mapping, like this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Individual" table="Individual">
    <id name="Id">
      <generator class="identity" />
    </id>
    <bag name="Accounts" cascade="all" table="IndividualAccount">
      <key column="Individual_id" />
      <many-to-many column="Account_id" class="Account" />
    </bag>
  </class>

  <class name="Account" table="Account">
    <id name="Id">
      <generator class="identity" />
    </id>
    <property name="Type" />
    <join table="IndividualAccount" inverse="true">
      <key column="Account_id" />
      <many-to-one name="Individual" column="Individual_id" />
    </join>
  </class>
</hibernate-mapping>

This seems to work, but I'm getting an extra "left outer join" to IndividualAccount when I execute the following HQL, which suggests to me that I'm not doing something right:
Code:
select
  distinct i
from
  Individual as i
  inner join i.Accounts as a
where
  a.Type = 'Checking'

--->

select distinct
    individual0_.Id as Id3_,
    individual0_.FirstName as FirstName3_,
    individual0_.LastName as LastName3_,
    individual0_.DOB as DOB3_,
    individual0_.Addr1 as Addr5_3_,
    individual0_.Addr2 as Addr6_3_,
    individual0_.City as City3_,
    individual0_.State as State3_,
    individual0_.Zip as Zip3_
from
    [Individual] individual0_
    inner join IndividualAccount accounts1_ on individual0_.Id=accounts1_.Individual_id
    inner join Account account2_ on accounts1_.Account_id=account2_.Id
    left outer join IndividualAccount account2_1_ on account2_.Id=account2_1_.Account_id
where
    (account2_.Type='Checking' )

What's the right way to be doing this? Thanks in advance.

- chad


Top
 Profile  
 
 Post subject: Re: One-To-Many with a Join Table
PostPosted: Mon May 18, 2009 7:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably need a many-to-many on Account, too (marked as inverse="true"). But you can map it as a private field instead of a property and use a property that returns the first (and only) item of that collection.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: One-To-Many with a Join Table
PostPosted: Thu May 21, 2009 10:35 pm 
Newbie

Joined: Sun May 17, 2009 2:14 pm
Posts: 4
Wolfgang --

Thanks for the reply! I've taken that approach and it seems to be working reasonably well -- it's generating the exact SQL that I would expect. However, what I lose is the ability to do HQL queries like this:
Code:
from Account a where a.Individual.FirstName='bob'

Is there way to overcome this?

- chad


Top
 Profile  
 
 Post subject: Re: One-To-Many with a Join Table
PostPosted: Fri May 22, 2009 1:52 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try

Code:
from Account a join fetch a.Individuals i where i.FirstName='bob'

_________________
--Wolfgang


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