-->
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: Interesting Mapping Problem
PostPosted: Wed Jul 21, 2004 4:52 am 
Newbie

Joined: Sun May 30, 2004 7:38 am
Posts: 7
Consider the following classes:

Code:
class Person {
  private PersonName personName;            // Current legal name of a Person
  Set otherPersonNames otherPersonNames;    // Historical names or aliases
  // Lots of other stuff
}

class PersonName {
  private String firstName;
  private String lastName;
}

class OtherPersonName extends PersonName {
  private Date validFrom;
  private Date validTo;
  private String use;
}


A Person always has one PersonName and potentially many PersonOtherName's, although usually none.

At first it seems appropriate to map Person and PersonName with a shared PK. It needs to be bi-directional
so a reference to Person is also added to PersonName as follows:

Code:
class Person {
  private String uuid;             // PK
  private PersonName personName;
  Set otherPersonNames otherPersonNames;
  ...
}

class PersonName {
  private String uuid;             // PK/FK
  private String firstName;
  private String lastName;
  private Person person;
}


This works, and cascades take care of cleaning up the associated PersonName if a Person is deleted.
As an aside, I'm interested to know if that reference back to Person in PersonName is actually needed
for bi-directional associations in the general case where two classes share a PK.

Now turning to OtherPersonName. It inherits the PK property from PersonName but it cannot share
a PK with Person because there could be many OtherPersonNames. So I introduced a separate PK as follows:

Code:
class OtherPersonName extends PersonName {
  private Long id;                // PK
  private Date validFrom;
  private Date validTo;
  private String use;
}


This works but OtherPersonName now has some excess baggage -- the uuid property inherited from PersonName.
I guess this isn't a big issue but it tends to spoil the model and I'm not happy about that.

I tried mapping PersonName/OtherPersonName as a component/collection-of-components in Person but that just breaks bi-directional functionality.

I also decoupled (no inheritance) PersonName and OtherPersonName and that works just great but creates a maintenance problem. For instance, if the size of firstName changes, two classes and mappings need to be changed.

What OtherPersonName seems to cry out for is a composite PK, composed of the inherited PK from PersonName in conjunction with an auto-generated key to make it unique. Is this possible or even recommended?

I am kinda stuck so would be very keen to hear your comments or alternative mapping strategies.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 8:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
PersonName is not a 1to1 mapping from an OO perspective because of you OtherPersonName inheritence

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 9:17 am 
Newbie

Joined: Sun May 30, 2004 7:38 am
Posts: 7
I see your point, thanks for the illumination.


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.