-->
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: Suggestions on how to solve a particular association
PostPosted: Fri Feb 27, 2004 10:14 am 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
I've had good success with Hibernate and my current model. I have a particular situation which actually occurs in a few places. I think i can figure out a way to "get it to work", but i would appreciate some suggestions of a solution that may make most sense.

Since i have the flexibility to change either the java class structure (somewhat) and/or the database, I'll try to explain it conceptually, then use an example.

Party object contains information about the Party. Name, SS#, etc. It's actually an abstract class with Business and Individual as derived classes.

Party can contain any number of Address objects of a certain address type. That is the key difference. An addressType can be mailing, residential, legal, etc. These addressTypes are stored internally as integers. There may only be one address of any one addressType attached to a Party. However the same address can be attached to a Party for any number of types. That is, the same address can be all known address Types.


Currently the methods in Party to handle this are:

void setAddress(Address address, int addressType)
Address getAddress(int addressType)

Party backs these methods with logic to insure only one addressType, and a Map.

================================================
Current status.

DB perspective.
Party table contains PartyID, Address Table contains AddressID
PartyAddress Table contains partyID,addressType,addressID with partyID,role as primary key (can will adjust if necessary)

Java

Assuming that i'd need a class for PartyAddress, I have an inner class Party$PartyAddress that contains addressType and Address object. I've sinced added a reference to the Party that owns it. (don't know if i need to, hoped i didn't).

Party contains a Map of all PartyAddress objects keyed by addressType.

Mapping File.
So, i'm figuring i would define a separate peristable class Party$PartyAddress. And have a collection of these guys inside Party. As i was creating the map for Party$PartyAddress, i then needed the primary key, then realized it needed to be composite, then realised the composite key was made up of the partyID inside the Party object in this class along with the addressType.
Wasn't sure how to handle that so was toying with adding a primary identity column to PartyAddress instead, and using that as primary key ...

which got me to wondering if i was even any where on the right track.

So, for any of you more experienced Hibernate users, i'm looking for suggestions. Maybe there is an easy pattern to use with Hibernate that i've just missed.

Note, i have a similar pattern/problem in other parts of the object model. A Vehicle object contains many Partys/Occupants of a certain type (owner, registrant etc).

so, this solution will be replicated elsewhere.

Please, any suggestions on how to handle this will be MUCH appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 10:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I think a <composite-element> for your association is just what you need. Take a look at the second example at http://www.hibernate.org/hib_docs/reference/html/components.html#components-s2-2. And yes, don't use inner classes, they will not work very nice (non-static inner classes can't be mapped at all).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 1:54 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Thanks for the help! I was looking at that scenario. Wasn't able to get it quite working and then i found that since my only extra attribute in the class was the key to the map, i went back to the simple. I guess i overcomplicated my requirement.

Code:
              <map name="addressMap" table="PARTY_ADDRESS" lazy="true">
                <key column="partyID"/>
                <index
                  column="addressType"
                  type="integer"
                  />
                  <many-to-many  column = "addressID" class="test.persist.Address"/>
              </map>


using many to many and it worked flawlessly. Thanks for the help. In my day or 2 of finding and trying this product out, i am extremely impressed.

This will definately solve our problems. I will now turn my attention to designing a DAO abstraction for our model suitable for our application.

Looking forward to working with it.


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.