| Oracle8i SQLJ Developer's Guide and Reference Release 2 (8.1.6) A81360-01 |
|
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:
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.
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.
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:
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.
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:
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.
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.
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:
SQLData interface readSQL() and writeSQL() methods
-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.
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:
-sql, -user, and -case options).
CustomDatum or SQLData; use the JPublisher -usertypes option).
-XXXtypes options: -numbertypes, -builtintypes, and -lobtypes).
-methods flag, which is enabled by default).
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.
For example, for Oracle object type home_address, JPublisher would create class HomeAddress in a HomeAddress.java source file.
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:
% jpub -sql=scott.Myobj,scott.mycoll:MyCollClass -user=scott/tiger
You cannot specify custom reference class names; JPublisher automatically derives them by adding Ref to custom object class names (relevant to CustomDatum implementations only). For example, if JPublisher produces Java source Myobj.java to define custom object class Myobj, then it will also produce Java source MyobjRef.java to define a MyobjRef custom reference class.
To create custom Java classes for the object and collection types defined in "User-Defined Types in the Database", you can run JPublisher as follows:
%jpub -user=scott/tiger -sql=Address,Person,Phone_array,Participant_t, Module_t,Moduletbl_t
Or, to explicitly specify the custom object class and custom collection class names:
%jpub -user=scott/tiger -sql=Address,Person,phone_array:PhoneArray, participant_t:ParticipantT,module_t:ModuleT,moduletbl_t:ModuletblT
(Each of the preceding two examples is a single wrap-around command line.)
The second example will produce Java source files Address.java, AddressRef.java, Person.java, PersonRef.java, PhoneArray.java, ParticipantT.java, ParticipantTRef.java, ModuleT.java, ModuleTRef.java, and ModuletblT.java. Examples of some of these source files are provided in "JPublisher Custom Java Class Examples".
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.
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.
(See "Supported Types for Host Expressions" for general information about how Oracle datatypes map to Java types.)
For more information about JPublisher features or options, see the Oracle8i JPublisher User's Guide.
JPublisher categorizes SQL types into the following groups, with corresponding JPublisher options as noted:
Use the JPublisher -usertypes option to specify the type-mapping implementation for UDTs--either a standard SQLData implementation or an Oracle-specific CustomDatum implementation.
NUMBER
Use the JPublisher -numbertypes option to specify type-mapping for numeric types.
BLOB and CLOB
Use the JPublisher -lobtypes option to specify type-mapping for LOB types.
CHAR, VARCHAR2, LONG, and RAW
Use the JPublisher -builtintypes option to specify type-mapping for built-in types.
JPublisher defines the following type-mapping modes, two of which apply to numeric types only:
jdbc)--Uses standard default mappings between SQL types and Java native types. For a custom object class, this specifies a SQLData implementation.
oracle)--Uses corresponding oracle.sql types to map to SQL types. For a custom object, reference, or collection class, this specifies a CustomDatum implementation.
objectjdbc)--This is an extension of JDBC mapping. Where relevant, object-JDBC mapping uses numeric object types from the standard java.lang package (such as java.lang.Integer, Float, and Double) instead of primitive Java types (such as int, float, and double). The java.lang types are nullable; the primitive types are not.
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.
Use the JPublisher -usertypes option to determine how JPublisher will implement the custom Java class that corresponds to a SQL object type:
-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.
-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.
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:
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.
Table 6-1 summarizes JPublisher categories for SQL types, the mapping settings relevant for each category, and the default settings.
|
Note:
The JPublisher |
In creating custom object classes to map Oracle objects to Java, the -methods option instructs JPublisher whether to include Java wrappers for Oracle object methods (member functions). The default -methods=true setting generates wrappers.
Wrapper methods generated by JPublisher are always instance methods, even when the original object methods are static. See "Custom Java Class Support for Object Methods" for more information.
The following example shows how to set the -methods option:
% jpub -sql=Myobj,mycoll:MyCollClass -user=scott/tiger -methods=true
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().
Alternatively, you can specify desired Java method names, but this requires use of a JPublisher input file and is discussed in "Creating Custom Java Classes and Specifying Member Names".
|
Note:
The |
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:
CREATE OR REPLACE TYPE my_type AS OBJECT( ... MEMBER FUNCTION myfunc(x INTEGER) RETURN my_return IS BEGIN ... END; MEMBER FUNCTION myfunc(y SMALLINT) RETURN my_return IS BEGIN ... END; ...);
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.)
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:
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.
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.
For further discussion about subclassing JPublisher-generated classes or using them as fields (continuing the preceding example), see "Extending Classes Generated by JPublisher".
JPublisher supports the use of special input files and standard properties files to specify type mappings and additional option settings.
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:
SQL udt1 <GENERATE GeneratedClass1> <AS MapClass1> SQL udt2 <GENERATE GeneratedClass2> <AS MapClass2> ...
This generates GeneratedClass1 and GeneratedClass2, but maps udt1 to MapClass1 and udt2 to MapClass2.
In the following example, JPublisher will pick up the -user option from the command line and go to input file myinput.in for type mappings.
Command line:
% jpub -input=myinput.in -user=scott/tiger
Contents of input file myinput.in:
SQL Myobj SQL mycoll AS MyCollClass SQL employee GENERATE Employee AS MyEmployee
This accomplishes the following:
MYOBJ gets the custom object class name Myobj because that's how you typed it--JPublisher creates source Myobj.java (and MyobjRef.java).
MYCOLL is mapped to MyCollClass. JPublisher creates a MyCollClass.java source file.
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.
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.
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.
Command line:
% jpub -props=jpub.properties -user=scott/tiger
Contents of properties file jpub.properties:
jpub.sql=Myobj,mycoll:MyCollClass,employee:Employee:MyEmployee jpub.usertypes=oracle
This produces the same results as the input-file example above, explicitly specifying the oracle mapping setting.
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:
SQL udt <GENERATE GeneratedClass> <AS MapClass> <TRANSLATE membername1 AS Javaname1> <, membername2 AS Javaname2> ...
(This is a single wrap-around command line.)
TRANSLATE pairs (membernameN AS JavanameN) are separated by commas.
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).
SQL employee GENERATE Employee AS MyEmployee TRANSLATE address AS HomeAddress, GIVE_RAISE AS giveRaise
(This is a single wrap-around command line.)
This section describes how JPublisher generates wrapper methods and how wrapper method calls are processed at runtime.
The following points describe how JPublisher generates wrapper methods:
-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.
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.
This section provides examples of JPublisher-generated CustomDatum implementations for the following user-defined types (created in "User-Defined Types in the Database"):
Address, corresponding to the Oracle object type ADDRESS) and related custom reference class (AddressRef)
ModuletblT, corresponding to the Oracle collection type MODULETBL_T)
For examples of JPublisher-generated
Note:
SQLData implementations, as well as further examples of JPublisher-generated CustomDatum implementations, see the Oracle8i JPublisher User's Guide.
Following is an example of the source code that JPublisher generates for a custom object class. Implementation details have been omitted.
In this example, unlike in "Creating Object Types", assume the Oracle object ADDRESS has only the street and zip_code attributes.
package bar; import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.STRUCT; import oracle.jpub.MutableStruct; public class Address implements CustomDatum, CustomDatumFactory { public static final String _SQL_NAME = "SCOTT.ADDRESS"; public static final int _SQL_TYPECODE = OracleTypes.STRUCT; public static CustomDatumFactory getFactory() { ... } /* constructor */ public Address() { ... } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { ... } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } /* accessor methods */ public String getStreet() throws SQLException { ... } public void setStreet(String street) throws SQLException { ... } public String getZipCode() throws SQLException { ... } public void setZipCode(String zip_code) throws SQLException { ... } }
Following is an example of the source code that JPublisher generates for a custom reference class to be used for references to ADDRESS objects. Implementation details have been omitted.
package bar; import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.REF; import oracle.sql.STRUCT; public class AddressRef implements CustomDatum, CustomDatumFactory { public static final String _SQL_BASETYPE = "SCOTT.ADDRESS"; public static final int _SQL_TYPECODE = OracleTypes.REF; public static CustomDatumFactory getFactory() { ... } /* constructor */ public AddressRef() { ... } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { ... } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } public Address getValue() throws SQLException { ... } public void setValue(Address c) throws SQLException { ... } }
Following is an example of the source code that JPublisher generates for a custom collection class. Implementation details have been omitted.
import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.ARRAY; import oracle.sql.ArrayDescriptor; import oracle.jpub.runtime.MutableArray; public class ModuletblT implements CustomDatum, CustomDatumFactory { public static final String _SQL_NAME = "SCOTT.MODULETBL_T"; public static final int _SQL_TYPECODE = OracleTypes.ARRAY; public static CustomDatumFactory getFactory() { ... } /* constructors */ public ModuletblT() { ... } public ModuletblT(ModuleT[] a) { ... } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { ... } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } public String getBaseTypeName() throws SQLException { ... } public int getBaseType() throws SQLException { ... } public ArrayDescriptor getDescriptor() throws SQLException { ... } /* array accessor methods */ public ModuleT[] getArray() throws SQLException { ... } public void setArray(ModuleT[] a) throws SQLException { ... } public ModuleT[] getArray(long index, int count) throws SQLException { ... } public void setArray(ModuleT[] a, long index) throws SQLException { ... } public ModuleT getObjectElement(long index) throws SQLException { ... } public void setElement(ModuleT a, long index) throws SQLException { ... } }
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 discussed in "Generate Custom Java Classes and Map Alternate Classes", the JPublisher syntax to generate JAddress but map to MyAddress is as follows:
-sql=ADDRESS:JAddress:MyAddress
or, in an input file:
SQL ADDRESS GENERATE JAddress AS MyAddress
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:
MyAddress class, instead of the JAddress class, is used to represent attributes whose database type is ADDRESS.
MyAddress class, instead of the JAddress class, is used to represent method arguments and function results whose type is ADDRESS.
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.
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.
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:
SQLData interface, then the subclass will inherit this implementation and the necessary readSQL() and writeSQL() functionality of the generated class.
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.
import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.STRUCT; import oracle.jpub.runtime.MutableStruct; public class JAddress implements CustomDatum, CustomDatumFactory { public static final String _SQL_NAME = "SCOTT.ADDRESS"; public static final int _SQL_TYPECODE = OracleTypes.STRUCT; public static CustomDatumFactory getFactory() { ... } /* constructor */ public JAddress() { ... } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { ... } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } /* shallow copy method: give object same attributes as argument */ void shallowCopy(JAddress d) throws SQLException { ... } /* accessor methods */ public String getStreet() throws SQLException { ... } public void setStreet(String street) throws SQLException { ... } public String getCity() throws SQLException { ... } public void setCity(String city) throws SQLException { ... } public String getState() throws SQLException { ... } public void setState(String state) throws SQLException { ... } public java.math.BigDecimal getZip() throws SQLException { ... } public void setZip(java.math.BigDecimal zip) throws SQLException { ... } }
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.
import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.REF; import oracle.sql.STRUCT; public class MyAddressRef implements CustomDatum, CustomDatumFactory { public static final String _SQL_BASETYPE = "SCOTT.ADDRESS"; public static final int _SQL_TYPECODE = OracleTypes.REF; public static CustomDatumFactory getFactory() { ... } /* constructor */ public MyAddressRef() { ... } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { ... } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } public MyAddress getValue() throws SQLException { ... } public void setValue(MyAddress c) throws SQLException { ... } }
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.
import java.sql.SQLException; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.STRUCT; import oracle.jpub.runtime.MutableStruct; public class MyAddress extends JAddress { /* _SQL_NAME inherited from MyAddress */ /* _SQL_TYPECODE inherited from MyAddress */ static _myAddressFactory = new MyAddress(); public static CustomDatumFactory getFactory() { return _myAddressFactory; } /* constructor */ public MyAddress() { super(); } /* CustomDatum interface */ /* toDatum() inherited from JAddress */ /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { ... } /* accessor methods inherited from JAddress */ /* Additional methods go here. These additional methods (not shown) are the reason that JAddress was extended. */ }