JPublisher and the Creation of Custom Java Classes

Oracle offers flexibility in how users can customize the mapping of Oracle object types, reference types, and collection types to Java classes in a strongly typed paradigm. Developers have the following choices in creating these custom Java classes:

using Oracle JPublisher to automatically generate custom Java classes and using those classes directly without modification

using JPublisher to automatically generate custom Java classes and subclassing them to create custom Java classes with added functionality

Although you have the option of manually coding your custom Java classes, using JPublisher is advisable. If you need special functionality, you can subclass a class that JPublisher generates.

JPublisher can implement either the Oracle oracle.sql.CustomDatum interface or the standard java.sql.SQLData interface when it generates a custom object class. If you choose the CustomDatum implementation, then JPublisher will also generate a custom reference class.

The SQLData interface is not intended for custom reference or custom collection classes. If you want your code to be portable, you have no choice but to use standard, weakly typed java.sql.Ref objects to map to references, and java.sql.Array objects to map to collections.

This manual provides only a minimal level of information and detail regarding the JPublisher utility. See the Oracle8i JPublisher User's Guide for more information.

What JPublisher Produces

When you use JPublisher to generate custom Java classes, you can use either a CustomDatum implementation (for custom object classes, custom reference classes, or custom collection classes) or a SQLData implementation (for custom object classes only). A CustomDatum implementation will also implement the CustomDatumFactory interface, for creating instances of the custom Java class.

This is controlled by how you set the JPublisher -usertypes option. A setting of oracle specifies a CustomDatum implementation; a setting of jdbc specifies a SQLData implementation.

CustomDatum Implementation

When you run JPublisher for a user-defined object type and choose the CustomDatum (and CustomDatumFactory) implementation for your custom object class, JPublisher automatically creates the following:

a custom object class to act as a type definition to correspond to your Oracle object type

This class includes getter and setter methods for each attribute. The method names are of the form getFoo() and setFoo() for attribute foo.

In addition, JPublisher by default will generate wrapper methods in your class that invoke the associated Oracle object methods executing in the server. This can be disabled, however, by setting -methods=false. This option is described later in this section.

This class includes a getValue() method that returns an instance of your custom object class, and a setValue() method that updates an object value in the database, taking as input an instance of the custom object class.

custom classes for any object or collection attributes of the top-level object

This is necessary so that attributes can be materialized in Java whenever an instance of the top-level class is materialized.

When you run JPublisher for a user-defined collection type, choosing the CustomDatum implementation, JPublisher automatically creates the following:

a custom collection class to act as a type definition to correspond to your Oracle collection type

This class includes overloaded getArray() and setArray() methods to retrieve or update a collection as a whole, a getElement() method and setElement() method to retrieve or update individual elements of a collection, and additional utility methods.

a custom object class for the elements, if the elements of the collection are objects

This is necessary so that object elements can be materialized in Java whenever an instance of the collection is materialized.

JPublisher-generated custom Java classes in any of these categories implement the CustomDatum interface, the CustomDatumFactory interface, and the getFactory() method.

Note:

If you specify the CustomDatum implementation, the generated classes will use Oracle-specific features and therefore will not be portable.

SQLData Implementation

As with the CustomDatum implementation, when you run JPublisher for a user-defined object type and choose the SQLData implementation for your custom object class, JPublisher will produce a custom object class to act as a type definition to correspond to your Oracle object type. This class will include the following:

getter and setter methods for each attribute

implementations of the standard SQLData interface readSQL() and writeSQL() methods

wrapper methods that invoke the Oracle object methods executing in the server (unless you specify -methods=false when you run JPublisher)

Because the SQLData interface is intended only for objects, however, and not for references or collections, JPublisher will not generate a custom reference class for references to the Oracle object type. You will have to use standard, weakly typed java.sql.Ref instances, or perhaps oracle.sql.REF instances if you do not require portability. Note that REF instances, like custom reference class instances, have Oracle extension methods getValue() and setValue() to read or write instances of the referenced object. Standard Ref instances do not have this functionality.

Similarly, because you cannot use a SQLData implementation for a custom collection class, you must use standard, weakly typed java.sql.Array instances, or perhaps oracle.sql.ARRAY instances if you do not require portability. Array and ARRAY instances, like custom collection class instances, have getArray() functionality to read the collection as a whole or in part, but do not have the element-level access and writability offered by the custom collection class getElement() and setElement() methods.

Note:

The SQLData interface is defined in the JDBC 2.0 specification to be portable. However, if you want the SQLData implementation produced by JPublisher to be portable, you must avoid using any Oracle-specific features and Oracle type mapping (which uses the Oracle-specific oracle.sql.* classes).

Generating Custom Java Classes

This section discusses key JPublisher command-line functionality for specifying the user-defined types that you want to map to Java and for specifying object class names, collection class names, attribute type mappings, and wrapper methods. These key points can be summarized as follows:

Specify user-defined types to map to Java. You can specify the custom object and custom collection class names for JPublisher to use, or you can accept the default names (use the JPublisher -sql, -user, and -case options).

Specify the implementation to use (CustomDatum or SQLData; use the JPublisher -usertypes option).

Choose whether or not JPublisher will create wrapper methods, in particular for Oracle object methods (use the JPublisher -methods flag, which is enabled by default).

Note:

Throughout this section, any discussion of custom reference classes or custom collection classes refers only to CustomDatum implementations.

Specify User-Defined Types to Map to Java

In using JPublisher to create custom Java classes, use the -sql option to specify the user-defined SQL types that you want to map to Java. You can either specify the custom object class names and custom collection class names, or you can accept the defaults.

The default names of your top-level custom classes--the classes that will correspond to the user-defined type names you specify to the -sql option--are identical to the user-defined type names as you enter them on the JPublisher command line. Because SQL names in the database are case-insensitive, you can capitalize them to ensure that your class names are capitalized per Java convention. For example, if you want to generate a custom class for employee objects, you can run JPublisher as follows:

% jpub -sql=Employee ...

The default names of lower-level classes, such as for home_address objects that are attributes of employee objects, are determined by the JPublisher -case option. If you do not set the -case option, it is set to mixed. This means that the default for the custom class name is to capitalize the initial character of the corresponding user-defined type name and the initial character of every word unit thereafter. JPublisher interprets underscores (_), dollar signs ($), and any characters that are illegal in Java identifiers as word-unit separators; these characters are discarded in the process.

Remember that Java class names are case-sensitive, although Oracle object and collection names (and SQL names in general) are not.

For backwards compatibility to previous versions of JPublisher, the -types option is still accepted as an alternative to -sql.

On the JPublisher command line, use the following syntax for the -sql option (you can specify multiple actions in a single option setting).

-sql=udt1<:mapclass1><,udt2<:mapclass2>>,...,<udtN<:mapclassN>> ...

And use the -user option to specify the database schema. Following is an example:

% jpub -sql=Myobj,mycoll:MyCollClass -user=scott/tiger

(There can be no space before or after the comma.)

For the Oracle object MYOBJ, this command will name it as you typed it, creating source Myobj.java to define a Myobj class. For the Oracle collection MYCOLL, this command will create source MyCollClass.java to define a MyCollClass class.

You can optionally specify schema names in the -sql option--for example, the scott schema:

So that it knows how to populate the custom Java classes, JPublisher connects to the specified schema (here, scott/tiger) to determine attributes of your specified object types or elements of your specified collection types.

If you want to change how JPublisher uses character case in default names for the methods and attributes that it generates, including lower-level custom Java class names for attributes that are objects or collections, you can accomplish this using the -case option. There are four possible settings:

-case=mixed (default)--The following will be uppercase: the first character of every word unit of a class name, every word unit of an attribute name, and every word unit after the first word unit of a method name. All other characters are in lowercase. JPublisher interprets underscores (_), dollar signs ($), and any characters that are illegal in Java identifiers as word-unit separators; these characters are discarded in the process.

-case=same--Character case is unchanged from its representation in the database. Underscores and dollar signs are retained; illegal characters are discarded.

-case=upper--Lowercase letters are converted to uppercase. Underscores and dollar signs are retained; illegal characters are discarded.

-case=lower--Uppercase letters are converted to lowercase. Underscores and dollar signs are retained; illegal characters are discarded.

Note:

If you run JPublisher without specifying the user-defined types to map to Java, it will process all user-defined types in the schema. Generated class names, for both your top-level custom classes and any lower-level classes for object attributes or collection elements, will be based on the setting of the -case option.

Specify Type Mappings

JPublisher offers several choices for how to map user-defined types and their attribute and element types between SQL and Java. The rest of this section lists categories of SQL types and the mapping options available for each category.

BigDecimal mapping (for numeric types only) (setting bigdecimal)--Uses java.math.BigDecimal to map to all numeric attributes; appropriate if you are dealing with large numbers but do not want to map to the oracle.sql.NUMBER type.

Note:

Using BigDecimal mapping can significantly degrade performance.

Mapping the SQL Object Type to Java

Use the JPublisher -usertypes option to determine how JPublisher will implement the custom Java class that corresponds to a SQL object type:

A setting of -usertypes=oracle (the default setting) instructs JPublisher to create a CustomDatum implementation for the custom object or collection class.

For a custom object class, this will also result in JPublisher producing a CustomDatum implementation for the corresponding custom reference class.

A setting of -usertypes=jdbc instructs JPublisher to create a SQLData implementation for the custom object class. No custom reference class can be created--you must use java.sql.Ref or oracle.sql.REF for the reference type.

This setting is invalid for implementing a custom collection class. (The SQLData interface is intended for mapping SQL object types only.)

The next section discusses type mapping options that you can use for object attributes and collection elements.

Mapping Attribute or Element Types to Java

If you do not specify mappings for the attribute types of a SQL object type or the element types of a SQL collection type, then JPublisher uses the following defaults:

For numeric types, the default mapping is object-JDBC.

For LOB types, the default mapping is Oracle.

For built-in type types, the default mapping is JDBC.

If you want alternate mappings, use the -numbertypes, -lobtypes, and -builtintypes options as necessary, depending on the attribute types you have and the mappings you desire.

If an attribute type is itself a SQL object type, it will be mapped according to the -usertypes setting.

Important:

Be especially aware that if you specify a SQLData implementation for the custom object class and want the code to be portable, you must be sure to use portable mappings for the attribute types. The defaults for numeric types and built-in types are portable, but for LOB types you must specify -lobtypes=jdbc.

Summary of SQL Type Categories and Mapping Settings

Table 6-1 summarizes JPublisher categories for SQL types, the mapping settings relevant for each category, and the default settings.

The JPublisher -mapping option used in previous releases will be deprecated but is currently still supported. For information about how JPublisher converts -mapping option settings to settings for the new mapping options, see the Oracle8i JPublisher User's Guide.

This will use default naming--the Java method names will be derived in the same fashion as custom Java class names (as described in "Specify User-Defined Types to Map to Java"), except that the initial character will be lowercase. For example, by default an object method name of CALC_SAL results in a Java wrapper method of calcSal().

The -methods option has additional uses as well, such as for generating wrapper classes for packages, or wrapper methods for package methods. This is beyond the scope of this manual--see the Oracle8i JPublisher User's Guide for information.

Regarding Overloaded Methods

If you run JPublisher for an Oracle object that has an overloaded method where multiple signatures have the same corresponding Java signature, then JPublisher will generate a uniquely named method for each signature. It accomplishes this by appending _n to function names, where n is a number. This is to ensure that no two methods in the generated custom Java class have the same name and signature. Consider, for example, the SQL functions defined in creating a MY_TYPE object type:

Without precaution, both definitions of myfunc result in the following name and signature in Java:

myfunc(Integer)

(Because both INTEGER and SMALLINT in SQL map to the Java Integer type.)

Instead, JPublisher might call one myfunc_1 and the other myfunc_2. (The _n is unique for each. In simple cases it will likely be _1, _2, and so on, but it might sometimes be arbitrary, other than being unique for each.)

Note:

How JPublisher handles overloaded wrapper methods applies to SQL functions created within an object or within a package, but not to top-level functions--overloading is not allowed at the top level.

Generate Custom Java Classes and Map Alternate Classes

You can use JPublisher to generate a custom Java class but instruct it to map the object type (or collection type) to an alternative class instead of to the generated class.

A typical scenario is to treat JPublisher-generated classes as superclasses, subclass them to add functionality, and map the object types to the subclasses. For example, presume you have an Oracle object type ADDRESS and want to produce a custom Java class for it that has functionality beyond what is produced by JPublisher. You can use JPublisher to generate a custom Java class JAddress for the purpose of subclassing it to produce a class MyAddress. Under this scenario you will add any special functionality to MyAddress and will want JPublisher to map ADDRESS objects to that class, not to the JAddress class. You will also want JPublisher to produce a reference class for MyAddress, not JAddress.

JPublisher has functionality to streamline the process of mapping to alternative classes. Use the following syntax in your -sql option setting:

-sql=object_type:generated_class:map_class

For the above example, use this setting:

-sql=ADDRESS:JAddress:MyAddress

This generates class JAddress in source file JAddress.java, but does the following:

Maps the object type ADDRESS to the MyAddress class, not to the JAddress class. Therefore, if you retrieve an object from the database that has an ADDRESS attribute, then this attribute will be created as an instance of MyAddress in Java. Or, if you retrieve an ADDRESS object directly, you will retrieve it into a MyAddress instance.

Creates a MyAddressRef class in MyAddressRef.java, instead of creating a JAddressRef class.

You must manually define a MyAddress class in a MyAddress.java source file. This class implements your required functionality by subclassing JAddress.

JPublisher Input Files and Properties Files

JPublisher supports the use of special input files and standard properties files to specify type mappings and additional option settings.

Using JPublisher Input Files

You can use the JPublisher -input command-line option to specify an input file for JPublisher to use for additional type mappings.

"SQL" in an input file is equivalent to "-sql" on the command line, and "AS" or "GENERATE...AS" syntax is equivalent to command-line colon syntax. Use the following syntax, specifying just one mapping per SQL command:

User-defined type EMPLOYEE is mapped to the MyEmployee class. JPublisher creates source Employee.java and MyEmployeeRef.java. If you retrieve an object from the database that has an EMPLOYEE attribute, this attribute would be created as an instance of MyEmployee in Java. Or if you retrieve an EMPLOYEE object directly, presumably you will retrieve it into a MyEmployee instance. You must manually create source file MyEmployee.java to define class MyEmployee, which would subclass the Employee class.

Using JPublisher Properties Files

You can use the JPublisher -props command-line option to specify a properties file for JPublisher to use for additional type mappings and other option settings.

In a properties file, "jpub." (including the period) is equivalent to the command-line "-" (single-dash), and other syntax remains the same. Specify only one option per line.

For type mappings, for example, "jpub.sql" is equivalent to "-sql". As on the command line, but unlike in an input file, you can specify multiple mappings in a single jpub.sql setting.

Properties File Example

In the following example, JPublisher will pick up the -user option from the command line and go to properties file jpub.properties for type mappings and the attribute-mapping option.

This produces the same results as the input-file example above, explicitly specifying the oracle mapping setting.

Note:

Unlike SQLJ, JPublisher has no default properties file. To use a properties file, you must use the -props option.

Creating Custom Java Classes and Specifying Member Names

In generating custom Java classes you can specify the names of any attributes or methods of the custom class. This cannot be specified on the JPublisher command line, however--only in a JPublisher input file using TRANSLATE syntax, as follows:

For example, presume the Oracle object type EMPLOYEE has an ADDRESS attribute that you want to call HomeAddress, and a GIVE_RAISE method that you want to call giveRaise(). Also presume that you want to generate an Employee class but map EMPLOYEE objects to a MyEmployee class that you will create (this is not related to specifying member names, but provides a full example of input file syntax).

When you specify member names, any members you do not specify will be given the default naming.

The reason to capitalize the specified attribute--HomeAddress instead of homeAddress--is that it will be used exactly as specified to name the accessor methods; getHomeAddress(), for example, follows naming conventions; gethomeAddress() does not.

JPublisher Implementation of Wrapper Methods

This section describes how JPublisher generates wrapper methods and how wrapper method calls are processed at runtime.

Generation of Wrapper Methods

The following points describe how JPublisher generates wrapper methods:

JPublisher-generated wrapper methods are implemented in SQLJ; therefore, whenever -methods=true, the custom object class will be defined in a .sqlj file, instead of in a .java file. Run SQLJ to translate the .sqlj file.

All wrapper methods generated by JPublisher are implemented as instance methods. This is because a database connection is required for you to invoke the corresponding server method. Each instance of a JPublisher-generated custom Java class has a connection associated with it.

Runtime Execution of Wrapper Method Calls

The following points describe what JPublisher-generated Java wrapper methods execute at runtime. In this discussion, "Java wrapper method" refers to a method in the custom Java object, while "wrapped SQL method" refers to the SQL object's method that is wrapped by the Java wrapper method.

The custom Java object is converted to a SQL object and passed to the database, where the wrapped SQL method is invoked. After this method invocation, the new value of the SQL object is returned to Java in a new custom Java object, either as a function return from the wrapped SQL method (if the SQL method is a stored procedure), or, if there already is a function return, as an array element in an additional output parameter (this is the case if the SQL method is a stored function).

Any output or input-output parameter is passed as the element of a one-element array. (This is to work around logistical issues with output and input-output parameters, as discussed in "Custom Java Class Support for Object Methods".) If the parameter is input-output, then the wrapper method takes the array element as input; after processing, the wrapper assigns the output to the array element.

Extending Classes Generated by JPublisher

You might want to enhance the functionality of a custom Java class generated by JPublisher by adding methods and transient fields. You can accomplish this by subclassing the JPublisher-generated class.

For example, suppose you want JPublisher to generate the class JAddress from the SQL object type ADDRESS. You also want to write a class MyAddress to represent ADDRESS objects and implement special functionality. The MyAddress class must extend JAddress.

Another way to enhance the functionality of a JPublisher-generated class is to simply add methods to it. However, adding methods to the generated class is not recommended if you anticipate running JPublisher at some future time to regenerate the class. If you run JPublisher to regenerate a class that you have modified in this way, you would have to save a copy and then manually merge your changes back in.

As a result of this, JPublisher will generate the REF class MyAddressRef (in MyAddressRef.java) rather than JAddressRef.

In addition, JPublisher alters the code it generates to implement the following functionality:

The MyAddress class, instead of the JAddress class, is used to represent attributes whose database type is ADDRESS.

The MyAddress class, instead of the JAddress class, is used to represent method arguments and function results whose type is ADDRESS.

The MyAddress factory, instead of the JAddress factory, is used to construct Java objects whose database type is ADDRESS.

You would presumably use MyAddress similarly in any additional code that you write.

At runtime, the Oracle JDBC driver will map any occurrences of ADDRESS data in the database to MyAddress instances, instead of to JAddress instances.

Requirements of Extended Classes

The class that you create (for example, MyAddress.java) must have a no-argument constructor. The easiest way to construct a properly initialized object is to invoke the constructor of the superclass, either explicitly or implicitly.

As a result of subclassing the JPublisher-generated class, the subclass will inherit definitions of the _SQL_NAME field, which it requires, and the _SQL_TYPECODE field.

In addition, one of the following will be true.

If the JPublisher-generated class implements the CustomDatum and CustomDatumFactory interfaces, then the subclass will inherit this implementation and the necessary toDatum(), and create() functionality of the generated class. You must implement a getFactory() method that returns an instance of your map class (such as a MyAddress object).

or:

If the JPublisher-generated class implements the SQLData interface, then the subclass will inherit this implementation and the necessary readSQL() and writeSQL() functionality of the generated class.

JPublisher-Generated Custom Object Class--JAddress.java

Continuing the example in the preceding sections, here is sample code for the JPublisher-generated class (JAddress), implementing CustomDatum and CustomDatumFactory. Implementation details have been omitted.

JPublisher-Generated Alternate Reference Class--MyAddressRef.java

Continuing the example in the preceding sections, here is sample code for the JPublisher-generated reference class (MyAddressRef, as opposed to JAddressRef, because MyAddress is the class that ADDRESS objects map to). This class also implements CustomDatum and CustomDatumFactory. Implementation details have been omitted.

Extended Custom Object Class--MyAddress.java

Continuing the example in the preceding sections, here is sample code for a MyAddress class that subclasses the JPublisher-generated JAddress class. The comments in the code show what is inherited from JAddress. Implementation details have been omitted.