-->
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: Mapping messy associations
PostPosted: Tue Jul 15, 2008 6:14 pm 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
Hi, I'm mapping my old (and poorly structured) database in hibernate. I have one table called Account, another called Fund, and a third called Balance.

Balance says how much money an account has in a given fund. So Account has a one-to-many relationship to Balance. Fund also has a one-to-many relationship to Balance.

So here's where it gets messy. Balance doesn't have a proper primary key. It has a foreign key to each of the two tables it is connected to. The two foriegn keys form a composite key on the table.

Oh, and the name of the foreign key to Fund is not the same name as the primary key on fund, but it holds the same data.

What is the proper way to write the mapping file?

Thank you in advance.


Top
 Profile  
 
 Post subject: Re: Mapping messy associations
PostPosted: Wed Jul 16, 2008 11:55 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
mojo_jojo wrote:
Hi, I'm mapping my old (and poorly structured) database in hibernate. I have one table called Account, another called Fund, and a third called Balance.

Balance says how much money an account has in a given fund. So Account has a one-to-many relationship to Balance. Fund also has a one-to-many relationship to Balance.

So here's where it gets messy. Balance doesn't have a proper primary key. It has a foreign key to each of the two tables it is connected to. The two foriegn keys form a composite key on the table.

Oh, and the name of the foreign key to Fund is not the same name as the primary key on fund, but it holds the same data.

What is the proper way to write the mapping file?

Thank you in advance.


You could use a composite key with a key-many-to-one mapping.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 1:04 pm 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
Thank you farzad. This appears to be what I need to do.

I do have one additional problem though. One of the columns has a different name on the two tables. So the table Balance has a column named fund and it references the column named code on the fund table. Does anyone know how to handle that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 2:17 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
mojo_jojo wrote:
Thank you farzad. This appears to be what I need to do.

I do have one additional problem though. One of the columns has a different name on the two tables. So the table Balance has a column named fund and it references the column named code on the fund table. Does anyone know how to handle that?


I am not sure if I understand the problem correctly (means I didn't read your post carefully) but I have a feeling you are looking for property-ref, by which you can specify this relationship is not based on primary keys (and similar) but the specified property-ref makes the relationship. Tell me if this is what you need.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 4:48 pm 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
In sql the relationship between the tables would be stated as "select * from fund f, balance b where f.code=b.fund" I'm not sure who to express this relationship in Hibernate. Most things I read assume that the columns have the same name on both tables.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 2:31 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
mojo_jojo wrote:
In sql the relationship between the tables would be stated as "select * from fund f, balance b where f.code=b.fund" I'm not sure who to express this relationship in Hibernate. Most things I read assume that the columns have the same name on both tables.


How does your mapping look like right now? I'd assume code is a primary key in fund then you would have something like:


Code:
In fund:

<id column="code"..>

In Balance:

<composite-id ....>
  <key-many-to-one name="fund" class="Fund" column="fund"/>
  ....
</composite-id>




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 4:22 pm 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
Thanks I guess it wasn't as complicated as I thought it was.


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.