I have a legacy schema with a particular table reference similar to:
Code:
Car
------------------
carID int
carName varchar(32)
colorID int
Color
------------------
colorID int
colorName varchar(32)
As expected, Car.colorID has a FK reference to Color.colorID.
I'd like to have a POJO which looks like:
Code:
@Entity
public class Car {
private int carID;
private String color;
@Id
public int getCarID() {
return carID;
}
public void setCarID(final int carID) {
this.carID = carID;
}
/* some clever annotations here */
public String getColor() {
return color;
}
public void setColor(final String color) {
this.color = color;
}
}
I'd be happy to use java 5 enums instead of Strings for color (indeed, this would be a marginal preference, though I suspect it would be more complicated). Although I've seen a few vaguely similar suggestions, I'd like to avoid tying colorIDs to Color enums in code (this DB is used by a large number of people, and I've got no guarantee that a well meaning soul won't change the colorIDs for any given color). If enums were used, they'd need to be instantiated using the colorName.
The other restriction is that, if someone calls car.setColor("BLUE"), the existing BLUE entry in the Color table is used, rather than creating a new BLUE entry with a new colorID.
The only thing I've seen which comes close is the @JoinTable annotation with @CollectionOfElements, although this obviously only works for collections of Strings, rather than just a single String. The other possibility is some sort of clever UserType, but being a relative hibernate beginner I thought I'd ask here before going too far down that road, in case such a thing has a simpler solution.
Apologies if this has been asked and answered before - having searched forums and articles I haven't got anywhere as yet. So, any ideas?
Many thanks.