[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
| 
Re: [eclipselink-users] XML attribute mapping to Java 5 Enum
 | 
Just as a followup,
After creating the EnumTypeConverter, you can override the default 
values, if you want your XML values to be something different than the 
String representation of your Enum value.
You would do something like:
EnumTypeConverter converter = new EnumTypeConverter(mapping, 
ValueType.class, false);
converter.addConversionValue("Max", "Maximum");
converter.addConversionValue("Min", "Minimum");
converter.addConversionValue("Nom", "Nominal");
-Matt
Leon Derks wrote:
For the ones interested in Java 5 enum conversion. We did this as 
follows:
We added an afterload method that adds an EnumTypeConverter.
It is important that the value of in the type attribute in XML matches 
the name of the Java enum.
By the way, is this the only way to do this? We couldn't find a way to 
get this done in the workbench.
See below for the details.
public static void valueAfterLoader(ClassDescriptor valueDescriptor) {
       //enum conversion
       DatabaseMapping mapping = 
valueDescriptor.getMappingForAttributeName("type");
       EnumTypeConverter converter = new EnumTypeConverter(mapping, 
ValueType.class, false);
       ((XMLDirectMapping) mapping).setConverter(converter);
   }
XML:
<Value type="Maximum" value="80"/>
public enum ValueType {
   Maximum("Maximum"), Minimum("Minimum"), Nominal("Nominal");
   private final String description;
   private ValueType(String description) {
       this.description = description;
   }
   public String value() {
       return description;
   }
   public String toString() {
       return description;
   }
}
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users