My preference would be to add a new option to @EnumType, say ATTRIBUTE, and additional parameters to @Enumerated, called say, AttributeGetter and AttributeType.
Example:
public enum Suit {
HEARTS("H"), DIAMONDS("D"), SPADES("S"), CLUBS("C");
private String code;
public Suit(String code)
{ this.code = code; }
public String getCode()
{ return this.code; }
}
@Enumerated(EnumType.ATTRIBUTE, AttributeGetter="getCode", AttributeType.STRING)
private Suit cardSuit;
Perhaps we could infer AttributeType from the return type of AttributeGetter.
Thanks
Andrew
On 27 Dec 2011, at 21:57, Andrew Ward <andreww100_at_gmail.com> wrote:
> Hi Expert Group
>
> The current JPA spec provides @Enumerated, with EnumType being limited to ORDINAL and STRING.
>
> ORDINAL is not good long term, as enumerations may be expanded, and ordinal positions will change if new literals are inserted. Seems too brittle.
> STRING is better than ORDINAL, but creates a conflict between the desire for short codes in the database, and meaningful literal names.
>
> What users really want is the ability to map an enum literal to a code (a short string, or number).
>
> The design problem you have, is how to best specify the custom enum attribute to be used as the code.
>
> EclipseLink and Hibernate understand this requirement, and provide proprietary solutions.
>
> http://wiki.eclipse.org/EclipseLink/Examples/JPA/EnumToCode
>
> http://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values
>
> Is there any chance of standardising this in JPA 2.1?
>
> Thanks
>
> Andrew Ward