Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/ObjectTypeConverter"
m |
m (→@ObjectTypeConverter Annotation) |
||
Line 2: | Line 2: | ||
|1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.mappings.converters.ObjectTypeConverter] | |1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.mappings.converters.ObjectTypeConverter] | ||
}} | }} | ||
− | =@ObjectTypeConverter Annotation= | + | =@ObjectTypeConverter Annotation and <object-type-converter> XML= |
You can use the <tt>@ObjectTypeConverter</tt> annotation to specify an <tt>org.eclipse.persistence.mappings.converters.ObjectTypeConverter</tt> that converts a fixed number of database data value(s) to Java object value(s) during the reading and writing of a mapped attribute. | You can use the <tt>@ObjectTypeConverter</tt> annotation to specify an <tt>org.eclipse.persistence.mappings.converters.ObjectTypeConverter</tt> that converts a fixed number of database data value(s) to Java object value(s) during the reading and writing of a mapped attribute. | ||
Line 97: | Line 97: | ||
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
− | |previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/TypeConverter| | + | |previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/TypeConverter|Type Converter]] |
− | |next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter| | + | |next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|Struct Converter]] |
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Default Conversions and Converters]] | |up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Default Conversions and Converters]] | ||
|version=2.1.0 | |version=2.1.0 | ||
}} | }} |
Revision as of 15:14, 17 June 2010
Key API {{{apis}}}
@ObjectTypeConverter Annotation and <object-type-converter> XML
You can use the @ObjectTypeConverter annotation to specify an org.eclipse.persistence.mappings.converters.ObjectTypeConverter that converts a fixed number of database data value(s) to Java object value(s) during the reading and writing of a mapped attribute.
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface ObjectTypeConverter { String name(); Class dataType() default void.class; Class objectType() default void.class; ConversionValue[] conversionValues(); String defaultObjectValue() default ""; }
This table lists attributes of the @ObjectTypeConverter annotation.
Attribute | Description | Default | Required or Optional |
---|---|---|---|
name |
Set this attribute to the String name for your converter. Ensure that this name is unique across the persistence unit |
no default |
required |
dataType |
Set this attribute to the type stored in the database. |
void.class1 |
optional |
objectType |
Set the value of this attribute to the type stored on the entity. |
void.class1 |
optional |
conversionValues |
Set the value of this attribute to the array of conversion values (instances of ConversionValue: String objectValue and String dataValue. See the Usage of the @ObjectTypeConverter Annotation example, to be used with the object converter. |
no default |
required |
defaultObjectValue |
Set the value of this attribute to the default object value. Note that this argument is for dealing with legacy data if the data value is missing. |
empty String |
optional |
1 The default is inferred from the type of the persistence field or property.
This example shows how to use the @ObjectTypeConverter annotation to specify the Employee field gender.
Usage of the @ObjectTypeConverter Annotation
public class Employee implements Serializable{ ... @ObjectTypeConverter ( name="genderConverter", dataType=java.lang.String.class, objectType=java.lang.String.class, conversionValues={ @ConversionValue(dataValue="F", objectValue="Female"), @ConversionValue(dataValue="M", objectValue="Male")} ) @Convert("genderConverter") public String getGender() { return gender; } ... }