# Representing an Oracle Object Type with a User-defined Domain

If you are not going to have a business components wizard generate domains for you, you need to create your own domain and assign it to an entity attribute.

#### To represent an Oracle Object Type:

1. Create a domain with the Domain Wizard.

   As you do, select the Domain for an Oracle Object Type checkbox to expose the list of Object Types available in the database, and then select the Object Type from the Available Types list.

   The JDeveloper Domain Wizard uses the object to create an object`.java` file in the package to represent the domain. The class encapsulates the object value and represents the Java class to which the Oracle Object is mapped. Unlike domains created from built-in SQL datatypes, the object`.java` file created for Oracle Objects is a mutable Java class. The object`.java` file contains getters and setters for all of the individual object attributes. For example, if you have a `NAME` object defined in SQL as:

       CREATE TYPE name AS OBJECT  -- user defined datatype
             (
              FirstName VARCHAR2(10),
              LastName VARCHAR2(15),
              );

   The Domain Wizard will create a `Name.java` file that contains the methods `getFirstName`, `setFirstName`, `getLastName`, and `setLastName`. You can then use `Name` as an attribute datatype in your programs to represent objects of type `NAME`.

2. Optionally modify the domain code, for example, add validation.

   The object`.java` file created by JDeveloper includes a `validate` method. You can add your own code to the validation method to ensure that the attribute conforms to the definition of the object. Later, when the object is used in your application, validation will be performed on the object whenever one is created or encountered. This allows the object to be referenced or reused in multiple places, and passed between tiers and applications, without the need for reconstruction or revalidation.

   The `validate` method does not extend to the individual attributes contained by the object. To provide validation at the attribute level, you can add your own code to the attribute getter and setter methods to validate the data that they work with.

3. In the Entity Object Wizard or Editor, assign the domain as a data type for an attribute.

## An Example

This example uses the `EMPLOYEE` table. The `EMPLOYEE` table contains employee name and ID columns and an Oracle Object `address_t` for the datatype of the employee `Address` column. The table and the datatype are defined as follows:

    CREATE TYPE address_t AS OBJECT  -- user defined datatype
          (
           Street VARCHAR2(30),
           City VARCHAR2(25),
           State CHAR(2),
           Zip  Number
           );

    CREATE TABLE Employee
          (
           EmpId Number,
           Name VARCHAR2(30),
           Address address_t,  -- note the Oracle Object datatype
           );

Note: | Alternatively, you can replace an Oracle Object Type column with a REF. |

#### To create a domain from the Oracle Object Type:

To create a domain using the Oracle Object type, follow these steps:

1. Right-click the Package and choose Create Domain to open the Domain Wizard.

2. In the Name panel of the Domain Wizard, enter the name of the domain you want to create or accept the default.

3. Select the Domain for an Oracle Object Type checkbox to expose the list of object types available in the database. Then select `Address_T` from the Available Types list and click Next.

4. In the Settings Panel, use the Attribute dropdown list to edit the properties of existing `Address_T` attributes. The domain attributes are automatically mapped to the Java Business Object datatypes (Street to a `String`, and so on). You can also use the Settings panel to remove attributes or create new attributes.

5. Click Finish. The wizard creates a reusable domain.

The `Address_T.java` class contains the getters and setters for each of the domain attributes: Street, City, State, and so on. Once the domain has been created, it can be reused as the datatype of an entity object attribute.

After you create the domain object to represent the Oracle Object, you can create the entity object for the table that uses the Oracle Object.

#### To create an entity object containing the Oracle Object Type:

Next, create the entity object for the table that contains the `Address_T` object.

1. Right-click the package and choose Create Entity Object to open the Entity Object Wizard.

2. Select the `EMPLOYEE` table to create the entity object.

3. Select the columns which are to be included as Entity attributes. Selecting the `Address` attribute will automatically set the `Address_T` Domain as the datatype of `Address`.

4. Click Finish to generate the `Employee` entity object.

An `Employee` entity object is created for the `EMPLOYEE` table. This entity object contains an `Address` attribute that has an `Address_T` domain as its datatype.

Related topics
Ways to Represent Oracle Object Types in Entity Objects
Representing a VARRAY that Contains Oracle Object Types
Replacing an Oracle Object Type with a REF to a Table of that Type
About Generating Entity Objects, Associations, and Database Tables
What Is an Entity Object?
What Is an Entity Attribute?
Business Component Data Types