ATTENTION !!! This Paper is Still Under Construction

 

Modeling and Mapping Objects to ObjectsVia

Java, JDBC and ORACLE

by: Siomara Cíntia Pantarotto.

 

Introduction

 

ORDBMS – Object-relational database management systems are able to store user-defined data types. This feature improves significantly the way how applications are developed, by allowing data structures to be reused, but they also create some complex management issues. This paper explains the key components and features of Java, as an object oriented programming language, and Oracle8, as an object-relational database, for implementing and mapping objects directly to objects via JDBC2, that supports a mechanism for automatically mapping structured SQL types.

 

Paper Contents

 

·        Understanding the Idea of Java and Oracle Objects

·        What is JDBC?

·        Basic JDBC Programming Concepts

·        Mapping Objects to Objects

·        Creating an Address Class in Java

·        Creating a Person Class in Java

·        Creating the Address and the Person Classes into Oracle8

·        Connecting to the Database and Managing the Mapping Process

·         Conclusion

 Understanding the Idea of Java and Oracle Objects?

An object is a collection of data values, also called fields or attributes, plus methods that operate on that data. In other words, an object is an abstract data type, created by a user to represent a specific data structure and perform some functionality. The data type of an object is called a class; an object is often referred to as an instance of its class. The class defines the type of each field or attribute in the object, and it provides the methods that operate on data contained in an instance of the class.

The principal concept of object oriented approach, is the encapsulation of data (attributes) and methods (behaviors) into objects. The data and methods of an object are intimately tied together. Objects have the property of information hiding. This means that although objects may know how to communicate with one another across well-defined interfaces, objects normally are not allowed to know how other objects are implemented. Implementation details are hidden within the objects themselves.

In Java, the unit of programming is the class from which objects are eventually instantiated. Functions do not disappear in Java; rather they are encapsulated as methods with the data they process within the “walls” of classes. Java programmers concentrate on creating their own user-defined types called classes. Classes are also referred to as programmer-defined types. Each class contains data as well as the set of methods that manipulate the data.

The best way to understand this concept is by analyzing the following use case model:

 

 

 

 

 

 

 

 

 This model shows a system that allows customers to place orders to purchase products from a seller, and this last one, also places orders in order to purchase products from a supplier to supply his stock.  Imagine how many objects we can abstract from this model. Not getting so deep, we can start by identifying two data structure that will certainly be used many times such as ADDRESS and PERSON. The system will need to register information about customers, suppliers, customer address, the address for delivery in case it’s different from the customer’s address, the supplier address, and many other address possibilities can come up after some more analysis. We can also identify others systems, such as human resource, financing, inventory, and many others, that will for sure make use of the same types of data structure, in different contexts.

The following chart shows that the object address is aggregated to both orders, purchase and supply, and is also part of the object called person.

 

 

 

 

 

 

 

 

 

 

 When modeling systems, using the OO approach, it’s important to identify classes that can be reused many times in many other contexts. It’s important to say that there are going to exist two basic types of classes. The first type is a programming class. This type of class is going to be used for programming purpose only. They hold data only when the system is active and making use of the objects that instantiated from them. We can identify a Java class as being a good example of a programming class. The other type of class is going to be used to create fields inside of database tables. Inn this case they will store information. This is a typical Oracle8 object.

 

What is JDBC?

 JDBC, that stands for Java database connectivity, is a package that allows programmers to connect to databases, query them, or update them, using the Structured Query Language, or SQL (SQL, usually pronounced like “sequel,” is an industry standard for database access.) In other word, JDBC is an interface to SQL, wich is the interface to essentially all modern relational databases. The JDBC package can be though of as nothing more than an application programming interface (API) for commuinicating SQL statements to databases.

Java and JDBC have an essential advantage over other database programming environments. Programs developed with Java programming language and JDBC are platform independent and vendor imdependent. The same database program written in Java programming language can run on a NT box, a Solaris server, or a database appliance powered by the Java platform. You can move your data from one database to another, for example, from Microsoft SQL Server to Oracle, or even to a tiny database embedded in a device, and the same program can still read your data. Because of their universatility, the java programming language and JDBC will eventually replace proprietary database languages and call level interfaces used by vendors such as Oracle, Informix, and Microsoft for acessing databases.

 

Basic JDBC Programming Concepts

 

 The four most important steps to access and retrieve information from a database via JDBC programming are: registering a database driver, using the DriverManager class to obtain a Connection object that represents a database connection, sending a sql statement to the database using the statement object, and receiving the results of a query with a ResultSet object.

 

Mapping Objects to Objects

 The word “mapping” is used to identify the process of transferring and retrieving data from and into to a database. When you map objects to objects, you are, actually, transferring information from an object oriented programming language object to an object oriented database or object-relational database, that has objects in its table’s structure, and vice versa. Mapping objects to objects, in this paper, means to transfer information from a Java object directly to an Oracle object or from an Oracle object directly to a Java object.

Although there are different approaches to map objects to other objects, this paper is going to demonstrate just one of them. There are some important steps that have to be followed in order to see the results. These steps are:

1.      Create a Java class to represent an address;

2.      Create a Java class to represent a person with an Address class as its attributes;

3.      Create the Address and Person classes into Oracle8;

4.      Create a Java class to connect the database and manage the mapping process.

 

Creating an Address Class in Java

 

 The following example shows a Java class that represents an address:

/**

 * File name: Address.java

 * This class represents an address.

 **/

public class Address {

     

      private String street;

      private String city;

      private String state;

      private String zip;

     

      /**

       * Constructors

       **/

      public Address() {}

      public Address(String street, String city,

   String state,  String zip)

{

            setStreet(street);

            setCity(city);

            setState(state);

            setZip(zip);           

      }

      /**

       * Methods to set street, city, state and zip

       **/

      public void setStreet(String street) { this.street = street; }

      public void setCity(String city)    { this.city   = city;   }

      public void setState(String state)  { this.state  = state;  }

      public void setZip(String zip)      { this.zip    = zip;    }

     

/**

       * Methods to get street, city, state and zip

       **/

      public String getStreet() { return street;  }

      public String getCity()   { return city;  }

      public String getState()  { return state; }

      public String getZip()    { return zip;   }

}

 

This class is composed of four private attributes, a method that constructs an object and methods that set values into the attributes and methods that return the values stored in the attributes.

 

Creating a Person Class in Java

 

The following example shows a Java class that represents a person:

/**

 * File name: Person.java

 * This class represents a person.

 **/

public class Person {

 

      private String name;

      private Address address; // Attribute of type Address

     

      /**

       * Constructors

       **/

      public Person() {}

      public Person(String newName, Address newAddress) {

            setName(newName);

            setAddress(newAddress);

      }

 

      /**

       * Methods to set name and address

       **/

      public void setName(String newName) {

            this.name = newName;

      }

      public void setAddress(Address newAddress) {

            this.address = newAddress;

      }

           

      /**

       * Methods to get name and address

       **/

      public String getName()     { return name; }

      public Address getAddress() { return address; }

}

 

This class is composed of two private attributes, a method that constructs an object and methods that set values into the attributes and methods that return the values stored in the attributes. Note that the class Person has the class Address, that was created before, as one of its private attributes.

 

Building the Address and Person Classes into Oracle

 

As already mentioned, one of the most common elements of most databases in most systems is the address. It is used in several places (for example, in ORDERS records, CUSTOMERS records, EMPLOYEES records, and INVOICE records). No matter where you encounter it, it has almost the same common elements following a standard format that is one or two ADDRESS lines, CITY, STATE, and ZIP. These information are used as the basis for an abstract datatype for addresses. In order to understand effectively what is a class, how it works, and the complex issues that it can create in the way data is managed, let’s create the Address class. First, use the create type command to create an abstract datatype as shown in the following lines:

 

create type ADDRESS_TY as object

(Street   VARCHAR2(50),

 City     VARCHAR2(30),

 State    CHAR(2),

 Zip      NUMBER);

 

The create type command is the most important command in object-relational databases. What the command in this example says is “create an abstract datatype named ADDRESS_TY. It will be represented as having four attributes, named Street, City, State, and Zip, using the defined datatypes and lengths for each column.” So far, the user has created no methods, but the database has internally created methods that will be used whenever the ADDRESS_TY object is accessed.

 

Within the create type command, note the as object clause. The as object clause explicitly identifies ADDRESS_TY as an OO implementation.

 

Now that the ADDRESS_TY datatype exists, it can be used within other datatypes. Going further, a company may decide to create a standard datatype for people. People have names and addresses. People can represent an employee, a customer, or any other kind of person that has a relationship with the company. Therefore, the company can create a datatype for person as shown bellow:

 

create type PERSON_TY as object

(Name     VARCHAR2(100),

 Address  ADDRESS_TY);

 

Again, the datatype was given a name – PERSON_TY – and identified as an object via the as object clause. Then, two columns were defined: This line:

 

(Name     VARCHAR2(100),

 

defines the first column of PERSON_TY’s representation and the second line:

 

Address   ADDRESS_TY);

 

defines the second column of PERSON_TY’s representation. The second column, Address, uses the ADDRESS_TY abstract datatype previously created.

Now, after seing these two simple examples, imagine how this capability to define and reuse abstract datatypes can simplify data representation within databases. Abstract datatypes allow you to join different elements together and deal with the whole instead of the parts that constitute the object.

Once the datatypes are created, it’s time to think of inserting records into database. You can’t insert data into PERSON_TY and the reason for that is straighfoward: a datatype describes data, it does not store data. To store data, you first have to create a table that uses your datatype and this is shown bellow:

 

create table EMPLOYEE

( CustomerID   NUMBER,

  Person       PERSON_TY);

 

Now with the table created its possible to store data into it. Oracle creates methods, called constructors methods, for data management when you create an abstract datatype. A constructor method is a program that is named after the datatype; its parameters are the names of the attributes defined for the datatype. When you need to insert records into a table based on abstract datatypes, you can use the constructor methods. For example, the EMPLOYEE table uses the PERSON_TY datatype, and the PERSON_TYdatatype uses the ADDRESS_TY datatype. In order to insert a record into the EMPLOYEE table, you will need to insert a record using the PERSON_TY and ADDRESS_TY datatypes. To insert records using these datatypes, you will need to use the constructor methods for the datatypes. In the following example, a record is inserted into EMPLOYEE using the constructor methods for the PERSON_TY and ADDRESS_TY datatypes. The constructor methods for these datatypes are shown in bold; they have the same names as the datatypes:

 

insert into EMPLOYEE values

(1,

 PERSON_TY(‘PATRIK SMITH’,

 ADDRESS_TY(‘45 MAIN STREET’,‘FREMONT’, ‘CA’, 96500));

 

 

If you use abstract datatypes to create tables, you can neither insert nor select values for the abstract datatypes attributes without knowing the exact structure of the attributes. For example, you cannot select the EMPLOYEE table’s City values unless you know that City is part of the Address attribute, and Address is part of the Person column. This is probably the complex issue that OO feature of Oracle8 creates when accessing data based on abstract datatypes. The following examples shows how to insert records in a table that has abstract dataypes columns:

 

            Select Person.Name,

            Person.Address.City

     from EMPLOYEE

     where Person.Address.City like ‘F%’;

 

 

PERSON.NAME        PERSON.ADDRESS.CITY

------------------- ---------------------------

PATRIK SMITH       FREMONT

 

    

When updating data within abstract datatypes, refer to its attributes via the Column.Attribute as shown in the preceding example. For example, to change the City value for a specific employee who live in Miami, FL, execute the following update command:

           

update EMPLOYEE

     set Person.Address.City = ‘NEW YORK’

     where Person.Address.City = ‘MIAMI’;

 

As shown in these examples, using abstract datatypes simplifies the representation of the data but may complicate the way in which you query and work with the data. It’s necessary to weigh the benefits of abstract datatypes – more intuitive representation of the data – against the potential increase in complexity of data access.

 

Connecting to the Database and Managing the Mapping Process

 

/*

 * This sample demonstrate basic Object support in the oci8 driver

 */

 

import java.sql.*;

import java.io.*;

import java.util.*;

import java.math.BigDecimal;

 

// this import is needed for Object Support

import oracle.sql.*;

 

// Importing Oracle Jdbc driver package makes the code more readable

import oracle.jdbc.driver.*;

 

public class PersonObject

{

  public static void main (String args [])

       throws Exception

  {

    // Register the Oracle JDBC driver

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

 

    // Connect to the database

    // You need to put your database name after the @ sign in

    // the connection URL.

    //

    // The sample retrieves an object of type "person",

    // materializes the object as an object of type ADT.

    // The Object is then modified and inserted back into the database.

 

    Connection conn =

      DriverManager.getConnection ("jdbc:oracle:oci8:@",

                                   "scott", "tiger");

 

    // It's faster when auto commit is off

    conn.setAutoCommit (false);

 

    // Create a Statement

    Statement stmt = conn.createStatement ();

 

    try

    {

      stmt.execute ("drop table people");

      stmt.execute ("drop type PERSON FORCE");

      stmt.execute ("drop type ADDRESS FORCE");

    }

    catch (SQLException e)

    {

      // the above drop and create statements will throw exceptions

      // if the types and tables did not exist before

    }

 

    stmt.execute ("create type ADDRESS as object (street VARCHAR (30), num NUMBER)");

    stmt.execute ("create type PERSON as object (name VARCHAR (30), home ADDRESS)");

    stmt.execute ("create table people (empno NUMBER, empid PERSON)");

 

    stmt.execute ("insert into people values (101, PERSON ('Greg', ADDRESS ('Van Ness', 345)))");

    stmt.execute ("insert into people values (102, PERSON ('John', ADDRESS ('Geary', 229)))");

 

    ResultSet rs = stmt.executeQuery ("select * from people");

    showResultSet (rs);

    rs.close();

 

    //now insert a new row

 

    // create a new STRUCT object with a new name and address

    // create the embedded object for the address

    Object [] address_attributes = new Object [2];

    address_attributes [0] = "Mission";

    address_attributes [1] = new BigDecimal (346);

 

    StructDescriptor addressDesc =

      StructDescriptor.createDescriptor ("ADDRESS", conn);

    STRUCT address = new STRUCT (addressDesc, conn, address_attributes);

 

    Object [] person_attributes = new Object [2];

    person_attributes [0] = "Gary";

    person_attributes [1] = address;

   

    StructDescriptor personDesc =

      StructDescriptor.createDescriptor("PERSON", conn);

    STRUCT new_person = new STRUCT (personDesc, conn, person_attributes);

 

    PreparedStatement ps =

      conn.prepareStatement ("insert into people values (?,?)");

    ps.setInt (1, 102);

    ps.setObject (2, new_person);

 

    ps.execute ();

    ps.close();

 

    rs = stmt.executeQuery ("select * from people");

    System.out.println ();

    System.out.println (" a new row has been added to the people table");

    System.out.println ();

    showResultSet (rs);

 

    rs.close();

    stmt.close();

    conn.close();   

  }

 

  public static void showResultSet (ResultSet rs)

    throws SQLException

  {

    while (rs.next ())

    {

      int empno = rs.getInt (1);

      // retrieve the STRUCT

      STRUCT person_struct = (STRUCT)rs.getObject (2);

      Object person_attrs[] = person_struct.getAttributes();

 

      System.out.println ("person name:  " +

                    (String) person_attrs[0]);

 

      STRUCT address = (STRUCT) person_attrs[1];

 

      System.out.println ("person address: ");

 

      Object address_attrs[] = address.getAttributes();

 

      System.out.println ("street:  " +

                      (String) address_attrs[0]);

      System.out.println ("number:  " +

                          ((BigDecimal) address_attrs[1]).intValue());

      System.out.println ();

    }

  }

}

 

Conclusion

 

            Object orientation is a universal technique that can be applied to many types of systems. Of late, this technique has found wide acceptance in the software industry. In other words, more and more areas of computer technology are converting on object orientation. There is now a consensus that acknowledges object orientation as the plataform most development organizations are choosing and will choose for the future.

 

            Even organizations that don’t have the software development as their main activity are concerned about how object orientation technology can impact in their business. They definetly want to stop focusing on computations and data storegment to focus on business problems or deal with technical issues at a high level of abstraction.

 

What we observe is that the benefits due to reuse, such as improved time to market, or higher quality systems, or lower overall development costs, increase as the levels of reuse and the sophistication of a reuse program increase. Furthermore, it takes time, investment, and experience with reuse to get to those levels of reuse.

 

For corporations that are willing to improve their business making use of OO technology it’s important to enphasize that they should adopt reuse systematically and incrementally before they can face success.

 

The adoption path common to several reuse atrategies is to start with focused pilot projects. As these pilots meet a degree of succcess, expand them incrementally, increasing reuse coverage and penetration into the organization.

 

To achieve systematic business reuse, an organization must keep this set of principles in mind:

1)      Maintain top-management leadership and financial backing over the long term.

2)      Plan and adapt the system architecture, the development process, and the organization to the necessities of reuse in a systematic but incremental fashion. Start with samall pilot projects, and then scale up.

3)      Plan for reuse beginning with the architecture and an incremental architecting process.

4)      Create and envolve reusable components in a real working environment.

5)      Realize that object or component technology alone is not sufficient

6)      Directly address organization culture and change.

7)      Invest in and continuosly improve infrastructure, reuse education, and skills.

8)      Measure reuse progress with metrics, and optimize the reuse program.

 

 


Bibliography

 

Deitel, H. M., and P.J. Deitel, C++ How to Program (Second edition), Upper Saddle River, NJ: Prentice Hall, 1998.

 

Jacobson, I., Ericsson, M., Jacobson, A., The Object Advantage – Business Process Reengineering With Object Technology, ACM Press, NY, 1995.

 

Jacobson, I., Griss, M., Jonsson, P., Software Reuse – Architecture, Process and Organization for Business Success, ACM Press, NY, 1997.

 

Koch, G., and Loney, K., ORACLE8 The Complete Reference,  Osborne/McGraw-Hill, CA, 1997.

 

Alban, H., Object-Oriented Technology, Tutorial Publication - docid 00016811, 1998.

 

David Flanagan, Jim Farley, Willian Crawford, Kris Magnusson, Java Enterprise in a Nutshell A Desktop Quick Reference, O’Reilly.

 

Cay S. Horstmann, Gary Cornell, Core Java Volume II – Advanced Features

 

David Flanagan, Java Examples in a Nutshell

 

Ayers D., Bergsten H., Bogovich M., Diamond J., Ferris M., Fleury M., Houle P., Mohseni P., Patzer A., Phillips R., Li S., Vedati K., Wilcox M., Zeiger S., Professional Java Server Programming

 

Horton I., Beginning Java 2 Programmer to Programmer, Wrox.

 

 

 

Web Links

 

Oracle Corporation

http://www.oracle.com

 

IEEE Society Studies and Proceeding on Object-oriented Topics

http://www.computer.org/epub/

 

Object Management Group

http://www.omg.com 1