< Prev - Examples - Next >

Inherited Objects

The object orientated programming in stored procedures is a little bit restricted. This example shows how to work with inherited object type and how they are represented as Java transfer object classes.

Example Description

Object Type Specification : BASE_OBJECT
create or replace type base_object force as object (
  d           date,
  ts          timestamp,
  instance    varchar2(100),
  db_version  number(9),
  db_release  number(9),
  member function val return varchar2  
) not final;
Object Type Body : BASE_OBJECT
create or replace type body base_object
as

member function val return varchar2
is
begin
  return 'd:'||to_char(d, 'yyyy/mm/dd')||'/ts:'||to_char(ts, 'yyyy/mm/dd hh:mi:ss')||'/instance:'||instance||'/db_version:'||db_version||'/db_release:'||db_release; 
end val;

end;
Java Transfer Object : BaseObject.java
/*
 * This file is generated by PL/SQL Enterprise Workbench Connector Builder.
 * PL/SQL Enterprise Workbench Copyright (c) Jan Richter, Hamburg, Germany, 2015-2016. All rights reserved.
 *
 * THIS FILE IS NOT INTENDED TO BE MODIFIED - IT WILL BE OVERWRITTEN ON EVERY RUN OF THE CONNECTOR BUILDER
 */

package transferobject;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

public class BaseObject implements Serializable {
  private static final long serialVersionUID = 1L;

  public Date d;
  public Timestamp ts;
  public String instance;
  public int dbVersion;
  public int dbRelease;
}
Object Type Specification : INHERITED_OBJECT
create or replace type inherited_object force under base_object (
  new_s    varchar2(100),
  new_ts   timestamp,
  new_d    date,
  new_n    number(9),
  overriding member function val return varchar2
);
Object Type Body : INHERITED_OBJECT
create or replace type body inherited_object
as

overriding member function val return varchar2
is
begin
  ret  urn (self as base_object).val || '/new_s:'||new_s||'/new_ts:'||to_char(new_ts, 'yyyy/mm/dd hh:mi:ss')||'/new_d:'||to_char(new_d, 'yyyy/mm/dd')||'/new_n:'||new_n; 
end val;

end;
Java Transfer Object : InheritedObject.java
/*
 * This file is generated by PL/SQL Enterprise Workbench Connector Builder.
 * PL/SQL Enterprise Workbench Copyright (c) Jan Richter, Hamburg, Germany, 2015-2016. All rights reserved.
 *
 * THIS FILE IS NOT INTENDED TO BE MODIFIED - IT WILL BE OVERWRITTEN ON EVERY RUN OF THE CONNECTOR BUILDER
 */

package transferobject;

import java.sql.Date;
import java.sql.Timestamp;

final public class InheritedObject extends BaseObject {
  private static final long serialVersionUID = 1L;

  public String newS;
  public Timestamp newTs;
  public Date newD;
  public int newN;
}
PL/SQL Package Specification : INHERITED_OBJECTS
create or replace package inherited_objects
as

function get_base_object return base_object;

function get_inherited_object(i_diff in number) return base_object;

function inspect_object(i_object in base_object) return varchar2;

end inherited_objects;
PL/SQL Package Body : INHERITED_OBJECTS
create or replace package body inherited_objects
as

function get_base_object return base_object
is
  o base_object;
begin
  o := base_object(null, null, null, null, null);
  o.d := sysdate;
  o.ts := systimestamp;
  select sys_context('USERENV','INSTANCE_NAME')
    into o.instance
    from dual;
  o.db_version := DBMS_DB_VERSION.VERSION;
  o.db_release := DBMS_DB_VERSION.RELEASE;
  return o;
end get_base_object;

function get_inherited_object(i_diff in number) return base_object
is
  o base_object;
begin
  select inherited_object(sysdate, 
                          systimestamp, 
                          sys_context('USERENV','INSTANCE_NAME'), 
                          DBMS_DB_VERSION.VERSION, 
                          DBMS_DB_VERSION.RELEASE, 
                          dbms_random.string('A', 100), 
                          systimestamp+i_diff, 
                          sysdate-i_diff, 
                          DBMS_RANDOM.value(0,999999999))
    into o
    from dual;  
  return o; 
end get_inherited_object;

function inspect_object(i_object in base_object) return varchar2
is
  result varchar2(1000);
begin
  if (i_object is of(only base_object)) then
    result := 'base_object ['|| i_object.val ||']';
  elsif (i_object is of(only inherited_object)) then
    result := 'inherited_object ['|| i_object.val ||']';
  else 
    result := 'null or unknown object';
  end if;
  
  return result;
end inspect_object;

end inherited_objects;
Java Calling Class : InheritedObjects.java
package plsql_workbench_examples;

import java.sql.Date;
import java.sql.Timestamp;

import factory.ServiceRPCFactory;
import service.InheritedObjectsService;
import transferobject.BaseObject;
import transferobject.InheritedObject;

public class InheritedObjects {
  public static void main(String[] args) {
    try {
      int diff = 10;
      InheritedObjectsService service = ServiceRPCFactory.getInheritedObjectsService();

      // create a base object from the database function
      BaseObject baseObject = service.getBaseObject();

      System.out.println(String.format("java-inspect: %s [d:%s/ts:%s/instance:%s/version:%d.%d]",
                                       baseObject.getClass(),
                                       baseObject.d,
                                       baseObject.ts,
                                       baseObject.instance,
                                       baseObject.dbVersion,
                                       baseObject.dbRelease));

      System.out.println("pl/sql inspect : " + service.inspectObject(baseObject));

      // create an inherited object from the database function
      baseObject = service.getInheritedObject(diff);
      InheritedObject inheritedObject = (InheritedObject) baseObject;
      System.out.println(String.format("java-inspect : %s [d:%s/ts:%s/instance:%s/version:%d.%d/newS:%s/newTs:%s/newD:%s/newN:%d]",
                                       inheritedObject.getClass(),
                                       inheritedObject.d,
                                       inheritedObject.ts,
                                       inheritedObject.instance,
                                       inheritedObject.dbVersion,
                                       inheritedObject.dbRelease,
                                       inheritedObject.newS,
                                       inheritedObject.newTs,
                                       inheritedObject.newD,
                                       inheritedObject.newN));

      System.out.println("pl/sql inspect : " + service.inspectObject(baseObject));

      // inspect of object instantiated in java
      inheritedObject = new InheritedObject();
      inheritedObject.d = new Date(System.currentTimeMillis());
      inheritedObject.instance = "any string";
      inheritedObject.ts = new Timestamp(System.currentTimeMillis());
      inheritedObject.dbVersion = 88;
      inheritedObject.dbRelease = 99;
      inheritedObject.newD = new Date(0);
      inheritedObject.newN = 1234567890;
      inheritedObject.newS = "another string";
      inheritedObject.newTs = new Timestamp((long) (Math.random() * 1000L * 3600L * 24L * 365L * 50L));
      System.out.println("pl/sql inspect () : " + service.inspectObject(inheritedObject));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}