Extending OracleAS TopLink to Support Custom Type Conversions Q: I would like to use a custom data type in my persistent model. I would like to avoid putting Transformation Mapping code in my model and the Object Type mapping doesn't provide me the "type" I require. How do I configure TopLink to use my custom type and convert between this type and database type of the underlying column?
Customize the TopLink runtime to use the new ConversionManager.
Map the attributes using TypeConversionMappings.
For the purpose of illustration this example will involve mapping a column of type VARCHAR(1) with three possible values (
Y = true,
N = false, and
NULL = unknown) to attributes of a custom class of
Answer class contains a single attribute of type
java.lang.Boolean. The use of a custom conversion to solve this allows for a more efficient solution then an
ObjectTypeMapping and allows for the static instances on
Answer to be used instead of a large quantity of instances. (
1. Extend the ConversionManager
TopLink performs all type conversions through the use of a
ConversionManager class (
oracle.toplink.internal.helper). This class can be extended to perform any additional conversions or to customize existing conversions. The source code to the
ConversionManager is shipped with the product in the source.jar library.
For the purpose of this example a subclass of
ConversionManager is created called
MyConversionManager. This new class must support converting from a String into an Answer instance and from an Answer instance into a String.
The important methods to overwrite are:
public Object convertObject(Object source, Class javaClass)
public String convertObjectToString(Object source)
For this particular case an additional method was added for converting into an Answer:
public Answer convertObjectToAnswer(Object source)
The ConversionManagerSetup is configured for use by the TopLink runtime through the
sessions.xml configuration file. The following line added to a session’s definition will cause the event listener to be registered and invoked. This assumes the use of SessionManager.