# About Domains

An entity or view attribute can have a domain as its data type, defining the type of values an attribute can have. A domain object is a developer-defined data type, an immutable Java class encapsulating a scalar value or a structure, with simple validation built into its constructor. The validation check occurs when an object of that domain type is created. Business Components for Java provides predefined domains, and you can create your own.

Some advantages of domains are:

- You can define custom data types to represent a database type that does not map to any business component data type or to further restrict values, such as to certain strings.

- You can define attribute settings for a domain, and then share them with all attributes that are based on the domain.

- Simple validations are performed once, against the domain definition, when the data is created. Then, the data object can be passed between the tiers without the need for reconstruction or revalidation.

## Use domains to define custom data types and maps

Business Components for Java uses Java data types. Relational database tables use built-in SQL types. The data types need to be mapped to each other so data can be passed back and forth.

Business Components for Java can make many of the mappings to SQL data types for you, either to a standard Java data type or to a predefined domain. For a list of supported mappings, see Business Component Data Types. During reverse generation, the wizard automatically assigns the default business component data types, which you can change, if needed. During forward generation, you use a wizard or editor to assign both the Java data type and the SQL data type (an appropriate default is provided), and later automatically create the tables containing the SQL data types.

If you have a data type that is not automatically mapped, such as some Oracle Object Types, you need to create a domain for it before you start reverse or forward generation.

Domain classes extend or encapsulate Oracle SQL data types. Domain objects can be converted to the standard JDBC data types.

## Tools help you create and use domains

JDeveloper provides a Domain Wizard and Editor for defining domains. It helps you specify a domain name, package, attribute settings, and properties, if used. After, you can add custom validation and formatting code, if needed. For example, you could create a domain named SSNumber, and write validation code for it that ensures that the resulting data type is a string of nine characters.

After you have created a domain object, you can use the Entity Object Wizard or Editor, View Object Wizard or Editor, or Attribute Editor to assign the domain to an attribute as its data type.

## Attributes inherit domain attribute settings

In the Domain Wizard and Editor, you can specify attribute settings so that all attributes of that domain type inherit those settings. Attributes may further restrict domain settings, but may not make them less restrictive. For example, a domain could be marked Persistent, meaning no transient attribute can be of that domain type.

## Domains can exist in the business logic tier or client

You can use a domain class on the thin client as well as the business logic tier to hold values of a specific type, such as a social security number. You can write code to format or validate the input string in the domain's validate method, which is called by all constructors. Domain classes can be associated fields in a form. The form could directly instantiate an instance of the domain class with the field's given input string.

## An example

Suppose you define a `Salary` domain and specify minimum and maximum values. Then, when program code creates a `Salary` object, the validation check ensures that the salary amount is within the range you specified.

    public void createSalary (Number amount) {

        try {
          // Assume MySalaryDomain is a domain defined elsewhere.
          MySalaryDomain salDom = new MySalaryDomain(amount);

          // Salary amount is OK. Continue.
          ...
        } catch (DataCreationException dce) {
            // Salary amount is invalid.
            ...
        }
    }

Related topics:

Business Component Data Types
Files that Define a Domain