-->
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: How to map these components?
PostPosted: Fri Jan 14, 2005 11:28 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
Hello Hibernate users!

I've got the following problem:

In my domain model I have person and address types. Each person always has a main address assigned, and also 0..2 other addresses. The total number of addresses can never exceed 3.

Relations are bi-directional, so all addresses know about the person they belong to.

I could map this as entity relations with a set containing all addresses and an additional one-to-one association for the main address, but I think it would be much faster to map this as components.

Of course the main address would be mapped as a simple address component, but how do I map the other 0..2 addresses so I don't have to care about the order or if they exist at all? Preferably this would be mapped as a set, wouldn't it?

Some help would be appreciated... :-)

Greetings
Gregor


Hibernate version: 2.1

Name and version of the database: MySQL 4.1 NDB Cluster


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 2:04 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Sounds like you have some complex behavior there. It's almost like you want an Addresses object that has a Primary address, and then a Set of no more than 2 alternative addresses.

You could settle for just modelling it as 3 Address components, but that would require all kinds of logic everywhere to check to see if the alternative addresses are blank.

Personally, I'd probably go for a custom UserType in this case. It's not that hard to do really wild and wooly mappings. I'd go for an object like:

public class Address {
... all your address, city, state accessor/mutator stuff here...
}

public class Addresses {
private Address primary;
private Set alternatives;
public Address getPrimary() { return primary; }
public setPrimary(Address p) { this.primary = p; }
public Set getAlternatives() { return alternatives; }
public void setAlternatives(Set s) { this.alternatives = s; }
}

Then, just create a CompositeUserType that reads all 15 or so fields from the data set and constructs your custom type. The only gotcha I've ever had is in storing this in the second level cache. Good luck! Post if you have trouble.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 3:42 pm 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
ok,thank your very much for your help, I'll see how it works :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 4:01 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Come to think of it, since you're using a UserType, you could map the alternate addresses as an array rather than a Set so that you can have stronger typing.

Something more like

Code:
public class Addresses {
  private Address primary;
  private Set alternatives;
  public Address getPrimary() { return primary; }
  public setPrimary(Address p) { this.primary = p; }
  public Address[] getAlternatives() { return alternatives; }
  public void setAlternatives(Address[] s) { this.alternatives = s; }
}


I think that'd be better, but personal choice!


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.