-->
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.  [ 1 post ] 
Author Message
 Post subject: Whats the best way to map an enum?
PostPosted: Wed Jan 31, 2007 1:40 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
Using hibernate 3.2.0GA, and Java1.5, what's the best way to map an enum? I've had a heck of a time trying to find it in the documentation, but, basically, this is what I have:

Code:
public enum YesNoType
{
    YES
    {
        public String getLabel()
        {
            return "Yes";
        }

        public Character getValue()
        {
            return new Character('Y');
        }
    },
    NO
    {
        public String getLabel()
        {
            return "No";
        }

        public Character getValue()
        {
            return new Character('N');
        }
    };

    public static YesNoType getType(Character c)
    {
        Set<YesNoType> types = EnumSet.allOf(YesNoType.class);
        for (YesNoType type : types)
        {
            if (type.getValue().equals(c))
                return type;
        }
        return null;
    }

    public static List<YesNoType> getAll()
    {
        Set<YesNoType> types = EnumSet.allOf(YesNoType.class);
        return new ArrayList<YesNoType>(types);
    }

    public abstract Character getValue();

    public abstract String getLabel();



And a persistent object that uses it:
Code:
@Entity
@Table(name = "CHST_COM")
@IdClass(CommodityPk.class)
public class Commodity extends SomeAbsClass implements Comparable
{
    //...
    private YesNoType               activeFlag;
    //...
    @Enumerated(EnumType.STRING)
    public YesNoType getActiveFlag()
    {
        return activeFlag;
    }
    //...
}


Now how do I get hibernate to look at the character value when persisting (and loading) the data from the db??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.