I tried putting this in the main forum, but it was mostly ignored. Since I'm using 3.2.0GA with annotations (and the latest versions of everything), I figure its a valid question for in this forum.
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??