Loading
Java Persistence API (JPA) Extensions Reference for EclipseLink, Release 2.4
  Go To Table Of Contents
Contents
 Search
Other Searches
 PDF
PDF
Comments
Comments


@WriteTransformer

Use @WriteTransformer on a TranformationMapping to transform a single attribute value to a single database column value. Use the @WriteTransformers annotation to wrap multiple transformations.


Annotation Elements

Table 2-80 describes this annotation's elements.

Table 2-80 @WriteTransformer Annotation Elements

Annotation Element Description Default

column

(Optional) The column into which the value should be written.

If a single WriteTransfomer annotates an attribute, the attribute's name will be used as the column name.

@javax.persistence.Column

method

(Optional) The String method name that the mapped class must have. This method returns the value to be written into the database column.

Note: To support DDL generation and returning policy, the method should be defined to return a particular type, not just an Object. For example:
public Time getStartTime()

The method may require @Transient to avoid being mapped as a Basic by default.


transformerClass

(Optional) User-defined class that implements the FieldTransformer interface. This will instantiate the class and use its buildFieldValue method to create the value to be written into the database column.

Note: To support DDL generation and returningpolicy, the method buildFieldValue in the class should be defined to return the relevant Java type, not just Object as defined in the interface. For example:
public Time buildFieldValue(Object instance, String fieldName, Session session).

void.class



NoteNote:

You must specify either transformerClass or method , but not both.



Usage

You cannot define a @WriteTransformer for a read-only mapping.

Unless the TransformationMapping is write-only, it should include a ReadTransformer that defines the transformation of the database column values into attribute values.

Configuring Field Transformer Associations

Using a FieldTransformer is non-intrusive; your domain object does not need to implement an EclipseLink interface or provide a special transformation method.

You can configure a method-based field transformer using AbstractTransformationMapping method addFieldTransformation, passing in the name of the database field and the name of the domain object method to use.

You can configure a class-based field transformer using AbstractTransformationMapping method addFieldTransformer, passing in the name of the database field and an instance of org.eclipse.persistence.mappings.Transfomers.FieldTransformer.

A convenient way to create a FieldTransformer is to extend FieldTransformerAdapter.


Examples

See "Using @Transformation Annotation" for an example of how to use the @WriteTransformer annotation with a Transformation mapping.


See Also

For more information, see:

Comments powered by Disqus