-->
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: Mapping collection of addresses with legacy table structures
PostPosted: Thu Dec 15, 2005 11:56 pm 
Newbie

Joined: Thu Dec 15, 2005 11:50 pm
Posts: 2
I am sure there is a simple answer to this that I am missing... I cannot seem to figure out how to map a collection of Addresses to other entities. I am working with a legacy database and am bound by the existing structures. My AddressBook table stores multiple addresses for other entities in my database. The table structures include an address_id field on the other entities. That field is 1 part of a composite id on the ADDRESS_BOOK table, the other part being sequence_number (just an incrementing number unique within each address_id). This allows a join between the two tables on address_id to result in a list of addresses for the entity. I have tried various combinations of lists, maps, sets and idbags on my other entities, but none seem to result in the correct table structure when I let NHibernate create a test DB. Is there any way I can map this collection of Addresses to the User and Organization objects???

Table structures look like this:

ADDRESS_BOOK
id --- PK
sequenceNumber --- PK

USER
id --- PK
address_id

ORGANIZATION
id --- PK
address_id


Classes are:

Address
AddressId (Class that has Id and SequenceNumber properties)

User
Id

Organization
Id


mapping files:

<class name="Address" table="ADDRESS_BOOK">
<composite-id name="id" class="AddressId" access="field">
<key-property name="id" type="Int32" length="8" access="field" />
<key-property name="sequenceNumber" type="Int32" length="3" access="field" />
</composite-id>
<properties......... />
</class>

<class name="User" table="USER">
<id name="id" type="Int32" length="8" unsaved-value="0" access="field">
<generator class="native" />
</id>
<??? SOME COLLECTION OF ADDRESSES HERE ???>
<properties......... />
</class>

<class name="Organization" table="ORGANIZATION">
<id name="id" type="Int32" length="8" unsaved-value="0" access="field">
<generator class="native" />
</id>
<??? SOME COLLECTION OF ADDRESSES HERE ???>
<properties......... />
</class>


Sample data:
ADDRESS_BOOK:
id sequence_number
10 1
10 2
10 3
10 4
11 1
12 1
12 2

USER
id address_id
1 10

ORGANIZATION
id address_id
1 11
2 12


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 2:13 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Does address_id have a unique value for each address in the AddressBook table? If so, you should just ignore the sequence_number field and pretend that address_id is the primary key (<id>).

If several addresses share the same address_id, you should use sequence_number as the primary key (<id>), map address_id to a separate property in Address class, and use property-ref attribute when mapping relations to Address in other classes.

Hope that helps...


Top
 Profile  
 
 Post subject: Thank you!
PostPosted: Tue Dec 20, 2005 1:23 am 
Newbie

Joined: Thu Dec 15, 2005 11:50 pm
Posts: 2
Sergey, thanks for your reply. I thought my account settings would email me when there was one, so I did not check till tonight.

address_id is not unique for each address in the AddressBook table. Each unique address_id essentially signifies a "group" of addresses that are further distinguished by their sequence_number. the address_id and sequence_number together make up a composite PK.

I will look into your property-ref attribute suggestion. thank you!


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.