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.