Skip Headers
Oracle® Application Development Framework Developer's Guide
10g Release 3 (10.1.3)
B25386-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

3.3 Creating Classes to Map to Database Tables

The TopLink map (.mwp file) contains the information required to represent database tables as Java classes. You can use the Create TopLink Map wizard or the Mapping editor to create this data, or manually code the file using Java and the TopLink API.

Use this information, or metadata, to pass configuration information into the run-time environment. The run-time environment uses the information in conjunction with the persistent entities (Java objects or EJB entity beans) and the code written with the TopLink API, to complete the application.

Figure 3-3 TopLink Metadata

This is a diagram of TopLink metadata interactions
Description of "Figure 3-3 TopLink Metadata"

Descriptors

Descriptors describe how a Java class relates to a data source representation. They relate object classes to the data source at the data model level. For example, persistent class attributes may map to database columns.

TopLink uses descriptors to store the information that describes how an instance of a particular class can be represented in a data source (see Section 3.4, "Mapping Classes to Tables"). Most descriptor information can be defined by TopLink, then read from the project XML file at run time.

Persistent Classes

Any class that registers a descriptor with a TopLink database session is called a persistent class. TopLink does not require that persistent classes provide public accessor methods for any private or protected attributes stored in the database.

3.3.1 How to Create Classes

To automatically create Java classes from your database table, use the Create Java Objects from Tables wizard. With this wizard you can create the following:

  • Java class for each table

  • TopLink map

  • Mapped attributes for each tables' columns

Figure 3-4 Create Java Objects from Tables Wizard

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-4 Create Java Objects from Tables Wizard"

After creating the initial Java classes and TopLink mappings, use the Mapping editor to customize the information. Refer to the Oracle JDeveloper online help for additional information.

3.3.2 What Happens when you Create a Class

After completing the Create Java Objects from Tables wizard JDeveloper creates a TopLink map and adds it to the project.

Figure 3-5 Navigation Window

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-5 Navigation Window"

The wizard will also create TopLink descriptor and mappings for each Java attribute (as defined by the structure and relationships in the database).

Figure 3-6 Structure Window

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-6 Structure Window"

3.3.3 What You May Need to Know

After creating a Java class from a database table, you can modify the generated TopLink descriptor and mappings. This section includes information on the following:

3.3.3.1 Associating Descriptors with Different Database Tables

The Create Java Objects from Tables wizard will associate the TopLink descriptor with a specific database table.

Use the Multitable Info tab in the Mapping editor (as shown in Figure 3-7) to associate an amendment method with a descriptor.

Figure 3-7 Sample Multitable Info Tab

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-7 Sample Multitable Info Tab"

3.3.3.2 Using Amendment Methods

You can associate a static Java method to be called when a descriptor is loaded at run time. This method can amend the run-time descriptor instance through the descriptor Java code API. Use this method to make some advanced configuration options that may not be currently supported by the TopLink.

The Java method must have the following characteristics:

  • Be public static.

  • Take a single parameter of type oracle.toplink.descriptors.ClassDescriptor.

Use the After Load tab in the Mapping editor (as shown in Figure 3-8) to associate an amendment method with a descriptor.

Figure 3-8 Sample After Load Tab

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-8 Sample After Load Tab"

3.3.3.3 Modifying the Generated Code

When using the Create Java Objects from Tables wizard, Oracle JDeveloper automatically generates the basic code for your Java classes.

Example 3-3 Sample Generated Java Class

package mypackage;
  import java.util.ArrayList;
  import java.util.List;

public class Address {
  /**Map employeeCollection <-> mypackage.Employee
   * @associates <{mypackage.Employee}>
   */

  private List employeeCollection;
  private Long addressId;
  private String pCode;
...