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?

A: The solution involves the following steps:

  1. Extend the ConversionManager.
  2. Customize the TopLink runtime to use the new ConversionManager.
  3. 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. The 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. ( Answer source)
 

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 source for MyConversionManager.java.
 

2. Customize the TopLink runtime to use the new ConversionManager

The new conversion manager must be installed into the runtime using the following code:

MyConversionManager cm = new MyConversionManager();
ConversionManager.setDefaultManager(cm);
event.getSession().getLogin().getPlatform().setConversionManager(cm);

The easiest way to install the conversion manager is to use a SessionEventListener . This listener will implement the preLogin event and register the new conversion manager as per the above code.

The source for ConversionManagerSetup.java.
 

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.

<event-listener-class>
oracle.toplink.example.conversion.ConversionManagerSetup
</event-listener-class>

The source for sessions.xml.
 

3. Map the attributes using TypeConversionMappings
Map the attributes from the persistent model to the relevant VARCHAR fields in the database schema using TypeConversionMappings. Ensure that the database type is set to java.lang.String.
Left Curve
Learn More
Right Curve
 · OracleAS TopLink News
 · Technical Information
 · How To
 · Documentation
Left Curve
Popular Downloads
Right Curve
Untitled Document