> It's unfortunate that you're forced to repeat all the possible enum values and their corresponding int > values.
@ExternalValues is a shorthand for @Externalizer and @Factory. Useful when you have handful of values to map. But if the value mapping is complicated or the extent is huge -- then @Externalizer and @Factory is the way to do it. The user needs to implement some classes in that case. Further complex mappings (say a single field value split into three columns or whatever) will require a full blown CustomMapping implementation. Essentially different degrees of capability areoffered with proportional effort from the user -- I do not see what is unfortunate about it. KARR, DAVID (ATTCINW) wrote: > >> -----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. > > ----- Pinaki -- View this message in context: http://n2.nabble.com/How-to-get-hardcoded-ordinal-values-to-map-to-specific-Enum-values-tp4183005p4209896.html Sent from the OpenJPA Users mailing list archive at Nabble.com.
