-->
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.  [ 2 posts ] 
Author Message
 Post subject: JoinTables for one-to-one element mappings
PostPosted: Wed Sep 06, 2006 8:04 am 
Newbie

Joined: Wed Sep 06, 2006 7:32 am
Posts: 6
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
a user type is what I would do in this case

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.