Notice: This Wiki is now read only and edits are no longer 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 |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{EclipseLink_UserGuide |
− | | | + | |info=y |
+ | |api=y | ||
+ | |apis= | ||
+ | *[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/ObjectTypeConverter.html @ObjectTypeConverter] | ||
}} | }} | ||
− | =@ObjectTypeConverter | + | =@ObjectTypeConverter= |
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 16: | Line 19: | ||
} | } | ||
</source> | </source> | ||
− | |||
− | <span | + | {{EclipseLink_AttributeTable |
− | + | |caption=<span>@ObjectTypeConverter Annotation Attributes</span> | |
− | + | |content= | |
− | + | <tr> | |
− | + | <td>'''<tt>name</tt>'''</td> | |
− | + | <td>Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit</td> | |
− | + | <td>no default</td> | |
− | + | <td>required</td> | |
− | + | </tr> | |
− | <tt>name</tt> | + | <tr> |
− | + | <td>'''<tt>dataType</tt>'''</td> | |
− | Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit | + | <td>Set this attribute to the type stored in the database.</td> |
− | + | <td><tt>void.class</tt><sup>1</sup></td> | |
− | no default | + | <td>optional</td> |
− | + | </tr> | |
− | required | + | <tr> |
− | + | <td>'''<tt>objectType</tt>'''</td> | |
− | + | <td>Set the value of this attribute to the type stored on the entity.</td> | |
− | <tt>dataType</tt> | + | <td><tt>void.class</tt><sup>1</sup></td> |
− | + | <td>optional</td> | |
− | Set this attribute to the type stored in the database. | + | </tr> |
− | + | <tr> | |
− | <tt>void.class</tt><sup>1</sup> | + | <td>'''<tt>conversionValues</tt>'''</td> |
− | + | <td>Set the value of this attribute to the array of conversion values (instances of <tt>ConversionValue</tt><nowiki>: </nowiki><tt>String objectValue</tt> and <tt>String dataValue</tt>. See the [[#Example: @ObjectTypeConverter Annotation|@ObjectTypeConverter Annotation]] example, to be used with the object converter.</td> | |
− | optional | + | <td>no default</td> |
− | + | <td>required</td> | |
− | + | </tr> | |
− | <tt>objectType</tt> | + | <tr> |
− | + | <td>'''<tt>defaultObjectValue</tt>'''</td> | |
− | Set the value of this attribute to the type stored on the entity. | + | <td>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.</td> |
− | + | <td>empty <tt>String</tt></td> | |
− | <tt>void.class</tt><sup>1</sup> | + | <td>optional</td> |
− | + | </tr> | |
− | optional | + | }} |
− | + | ||
− | + | ||
− | <tt>conversionValues</tt> | + | |
− | + | ||
− | Set the value of this attribute to the array of conversion values (instances of <tt>ConversionValue</tt><nowiki>: </nowiki><tt>String objectValue</tt> and <tt>String dataValue</tt>. See the [[#Example | + | |
− | + | ||
− | no default | + | |
− | + | ||
− | required | + | |
− | + | ||
− | + | ||
− | <tt>defaultObjectValue</tt> | + | |
− | + | ||
− | 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 <tt>String</tt> | + | |
− | + | ||
− | optional | + | |
− | + | ||
<br><sup>1</sup> The default is inferred from the type of the persistence field or property. | <br><sup>1</sup> The default is inferred from the type of the persistence field or property. | ||
− | + | The following example shows how to use the <tt>@ObjectTypeConverter</tt> annotation to specify the <tt>Employee</tt> field <tt>gender</tt>. | |
− | <span id="Example | + | <span id="Example: @ObjectTypeConverter Annotation"></span> |
− | '' | + | ======''Example: @ObjectTypeConverter Annotation''====== |
<source lang="java"> | <source lang="java"> | ||
public class Employee implements Serializable{ | public class Employee implements Serializable{ | ||
Line 95: | Line 79: | ||
} | } | ||
</source> | </source> | ||
+ | |||
+ | You can use the <tt><object-type-converter></tt> element in the deployment descriptor as an alternative to using the <tt>@ObjectTypeConverter</tt> annotation in the source code, as shown in the following example: | ||
+ | |||
+ | ======''Example: <object-type-converter> Element ''====== | ||
+ | <source lang="xml"> | ||
+ | <object-type-converter name="gender-converter" | ||
+ | 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> | ||
+ | </source> | ||
+ | |||
+ | |||
+ | |||
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
Line 100: | Line 98: | ||
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|@StructConverter]] | |next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|@StructConverter]] | ||
|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. | + | |version=2.2.0 DRAFT |
}} | }} |
Latest revision as of 10:05, 5 May 2011
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
@ObjectTypeConverter
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 ""; }
Attribute | Description | Default | Required? |
---|---|---|---|
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 @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.
The following example shows how to use the @ObjectTypeConverter annotation to specify the Employee field gender.
Example: @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; } ... }
You can use the <object-type-converter> element in the deployment descriptor as an alternative to using the @ObjectTypeConverter annotation in the source code, as shown in the following example:
Example: <object-type-converter> Element
<object-type-converter name="gender-converter" 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>