> -----Original Message-----
> From: Pinaki Poddar [mailto:[email protected]]
> Sent: Wednesday, December 23, 2009 6:20 AM
> To: [email protected]
> Subject: Re: How to get hardcoded ordinal values to map to specific
> Enum values?
> 
> 
> Hi,
>   The simple way to do this is via @ExternalValues. Unfortunately,
> @ExternalValues did not support enum types. With latest commit on
trunk
> revision 893504 [1], that support has been added.
> 
>   Here is an example of enum valued field stored as numbers in
> database.
> 
> public class Movie {
>     public enum Rating {GOOD, BAD, UGLY};
> 
>     @Id
>     private String title;
> 
>    /** need not be contiguous.
>      *  Specify as key=value pairs where key is the Enum and value is
> the
> integer in the database
>      *  @Type denotes the type stored in database
>     */
>     @ExternalValues({"GOOD=5", "BAD=2", "UGLY=1"})
>     @Type(int.class)
>     private Rating rating;
> 
>    Query parameter will work as enum:
>        em.createQuery("select m from Movie m where m.rating=:rating")
>            .setParameter("rating", Movie.Rating.GOOD)
>            .getResultList();

That's definitely a different way of doing it.  I don't know that it's
better, but it might be easier.  It's unfortunate that you're forced to
repeat all the possible enum values and their corresponding int values.
I wish again that OpenJPA or JPA in general had an extension mechanism
for the orm.xml.  This is something that might be better in XML
configuration than annotations.

> KARR, DAVID (ATTCINW) wrote:
> >
> > I'm trying to map a field that is essentially an enumerated type.
> The
> > ordinal values are stored in the DB.  I can specify
> > "@Enumerated(EnumType.ORDINAL)" on the field, and then in the
> definition
> > of my Java enumerated type, I can define the possible values I can
> > expect.  What seems to be missing here is that I have to map
specific
> > ordinal values.  I can't just assume the first value maps to "0",
and
> so
> > on.  I don't see an obvious way to define an enumerated type where I
> can
> > set the ordinal values.  Am I missing something simple here?
> >
> >
> 
> 
> -----
> Pinaki
> --
> View this message in context: http://n2.nabble.com/How-to-get-
> hardcoded-ordinal-values-to-map-to-specific-Enum-values-
> tp4183005p4208794.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to