Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/EnumToCode"

(New page: == How to map Enum to coded values == The JPA specification allows for mappings of Enums to database columns using the @Enumerated where the database value is either the Enum's name or or...)
 
m (References)
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== How to map Enum to coded values ==
+
[[Category:EclipseLink/Example/JPA|EnumToCode]]
  
The JPA specification allows for mappings of Enums to database columns using the @Enumerated where the database value is either the Enum's name or ordinal value. EclispeLink allows a developer to map an enum to coded values as well using a converter.  
+
== How to Map Enum to Coded Values ==
 +
 
 +
The JPA specification allows for mappings of Enums to database columns using the @Enumerated where the database value is either the Enum's name or ordinal value. EclipseLink allows a developer to map an enum to coded values as well using a converter.  
  
 
In this example the enum Gender(MALE, FEMALE) is mapped to a single character in the database where M=MALE and F=FEMALE.
 
In this example the enum Gender(MALE, FEMALE) is mapped to a single character in the database where M=MALE and F=FEMALE.
Line 11: Line 13:
 
<source lang=java>
 
<source lang=java>
 
public enum Gender {
 
public enum Gender {
Female, Male, ;
+
    Female,  
 +
    Male
 
}
 
}
 
</source>
 
</source>
Line 28: Line 31:
  
 
<source lang=xml>
 
<source lang=xml>
<object-type-converter name="gender" object-type="model.Gender"
+
<object-type-converter name="gender" object-type="model.Gender" data-type="java.lang.String">
data-type="java.lang.String">
+
<conversion-value object-value="Male" data-value="M" />
<conversion-value object-value="Male" data-value="M" />
+
<conversion-value object-value="Female" data-value="F" />
<conversion-value object-value="Female" data-value="F" />
+
</object-type-converter>
</object-type-converter>
+
 
</source>
 
</source>
  
Line 40: Line 42:
  
 
<source lang=java>
 
<source lang=java>
@Basic
+
@Basic
@Convert("gender")
+
@Convert("gender")
private Gender gender = Gender.Male;
+
private Gender gender = Gender.Male;
 
</source>
 
</source>
  
Line 48: Line 50:
  
 
<source lang=xml>
 
<source lang=xml>
<basic name="gender">
+
<basic name="gender">
<column name="GENDER" />
+
<column name="GENDER" />
<convert>gender</convert>
+
<convert>gender</convert>
</basic>
+
</basic>
 
</source>
 
</source>
 +
 +
 +
==References==
 +
* [http://blog.xebia.com/2009/08/28/using-enums-with-jpa-without-the-evil-ordinal/ An article on insulating your JPA entities from changes to the enum values in database]

Latest revision as of 21:56, 11 February 2011


How to Map Enum to Coded Values

The JPA specification allows for mappings of Enums to database columns using the @Enumerated where the database value is either the Enum's name or ordinal value. EclipseLink allows a developer to map an enum to coded values as well using a converter.

In this example the enum Gender(MALE, FEMALE) is mapped to a single character in the database where M=MALE and F=FEMALE.

Gender Enum

The Gender enum in this example is defined as:

public enum Gender {
    Female, 
    Male
}

Defining the Converter

An ObjectTypeConverter is used and can be defined in an annotation and any entity class of persistence unit. It requires a unique name across all converters in this persistence unit.

@ObjectTypeConverter(name = "gender", objectType = Gender.class, dataType = String.class, conversionValues = {
		@ConversionValue(objectValue = "Male", dataValue = "M"),
		@ConversionValue(objectValue = "Female", dataValue = "F") })

Alternatively the converter can be defined in an EclipseLink specific ORM XML file as:

<object-type-converter name="gender" object-type="model.Gender"	data-type="java.lang.String">
	<conversion-value object-value="Male" data-value="M" />
	<conversion-value object-value="Female" data-value="F" />
</object-type-converter>

Using the Converter

The converter can then be used on any attribute in the domain model of the given enum type.

@Basic
@Convert("gender")
private Gender gender = Gender.Male;

Or in the ORM.XML:

<basic name="gender">
	<column name="GENDER" />
	<convert>gender</convert>
</basic>


References