blob: 234589cbeb5b80f783ae897f109e28f23a122fe1 [file] [log] [blame]
Title: Extended Types
<P>JDBC specification defines a set of &quot;standard&quot; database column types (defined in java.sql.Types class) and a very specific mapping of these types to Java Object Types, such as java.lang.String, java.math.BigDecimal, etc. Sometimes there is a need to use a custom Java type not known to JDBC driver. CayenneModeler allows to configure an arbitrary Java class as an <TT>org.apache.cayenne.map.ObjAttribute</TT> type by simply entering a fully-qualified name such class in the type column of an ObjAttribute. However there is more to it than just that. Cayenne needs to know how to instantiate this type from a database &quot;primitive&quot; value, and conversly, how to transform an object of the custom type to a JDBC-compatible object.</P>
<H3><A name="ExtendedTypes-SupportingNonStandardTypes"></A>Supporting Non-Standard Types</H3>
<P><TT>org.apache.cayenne.access.types.ExtendedType</TT> interface serves to integrate a custom attribute type to Cayenne. An implementation must provide <TT>ExtendedType.getClassName()</TT> method that returns a fully qualified Java class name for the supported custom type, and a number of methods that convert data between JDBC and custom type. Configuring an ExtendedType is has in the code, some time during Cayenne startup. The following code sample demonstrates this procedure:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-comment">// create custom ExtendedType instance
</SPAN>ExtendedType customType = <SPAN class="code-keyword">new</SPAN> MyCustomType();
<SPAN class="code-comment">// Find DataNode
</SPAN>DataDomain domain = Configuration.getSharedConfiguration().getDomain();
<SPAN class="code-comment">// replace 'node_name' with the name of the DataNode you've entered in the Modeler.
</SPAN>DataNode node = domain.getNode(<SPAN class="code-quote">&quot;node_name&quot;</SPAN>);
<SPAN class="code-comment">// install ExtendedType
</SPAN>node.getAdapter().getExtendedTypes().registerType(customType);</PRE>
</DIV></DIV>
<H3><A name="ExtendedTypes-DbAdaptersandExtendedTypes"></A>DbAdapters and Extended Types</H3>
<P>As shown in the example above, ExtendedTypes are stored by <A href="dbadapter.html" title="DbAdapter">DbAdapter</A>. In fact DbAdapters often install their own extended types to address incompatibilities, incompleteness and differences between JDBC drivers in handling &quot;standard&quot; JDBC types. For instance some drivers support reading large character columns (CLOB) as java.sql.Clob, but some other - as &quot;character stream&quot;, etc. Adapters provided with Cayenne override <TT>configureExtendedTypes()</TT> method to install their own types, possibly substituting Cayenne defaults. Custom DbAdapters can use the same technique.</P>
<H3><A name="ExtendedTypes-ROPConsiderations"></A>ROP Considerations</H3>
<P>If you are using Cayenne in a three tier (ROP) environment, serialization of the extended type becomes important. More information can be found <A href="remote-object-persistence-customization.html" title="Remote Object Persistence Customization">here.</A></P>