Chapter 4. Database Objects
The object-oriented paradigm for computer programming languages was developed at Xerox PARC in the 1970s. Languages such as Smalltalk, Eiffel, and C++ were among the first to allow programmers to use objects. With the release of Oracle8, object-oriented features became available within the Oracle database. In most object-oriented programming languages, objects are instantiated from a class. The same is true in Oracle, but the nomenclature is a little different. In Oracle, a class is known as an object type, and it's from an object type that actual database objects are instantiated. Object types may contain attributes defined using the built-in Oracle types such as VARCHAR2 and NUMBER, and they may also contain attributes defined using other user-defined object types.
In Chapter 2 you saw how to create database tables, PL/SQL procedures, and PL/SQL functions. You will recall that table columns and PL/SQL variables may be defined using the built-in Oracle types. Oracle's object support also allows you to define columns and variables using object types. This is useful because it allows you to define columns that should contain composites of the built-in types. For example, suppose you needed to store an address that is made up of a street, city, state, and zip code. You could create separate columns in a table to store each of those address elements, but what if you needed to store another address in a different table? You could redefine those same columns in that other table, but then consider what would happen if you needed to add another element to the address: you would have to define a new column in each table. A better approach would be to define an object type that contains an attribute for each of the address elements, and then use this object type to define address columns in both tables. Then, when you need to add another element to the address, you need only modify the address object type, and the change will be made everywhere the object type is used.
In addition to attributes such as city and state, an object type can also contain procedures and functions. Such procedures and functions are known as object methods. One example of an object method is a function that calculates the age of a person based on the date of birth stored in one of that object's attributes. You'll see just such a method created later in this chapter.
In order to access database objects using SQLJ, you must create special custom classes. These custom classes mirror the database types on which the database objects are based, and are used to declare host objects in your SQLJ program. A host object is similar to a host variable. It resides locally in your Java program and may be used to access database objects.
In this chapter, you will learn how to create database object types, create and populate column objects and object tables, create custom classes, and write statements using SQL and SQLJ to access database objects. The SQL DDL and DML statements that create and populate the database objects and tables referred to in this chapter are contained in the SQL*Plus script object_user_schema.sql. You should run this script, or ask your DBA to run it, if you wish to run the example programs contained in this chapter.