Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Creating a Table Creator

You can automatically generate a `TableCreator` using:

- TopLink Workbench during development (see "Using TopLink Workbench During Development")

- `DefaultTableGenerator` at run time (see "Using the Default Table Generator at Run Time")

After creating a `TableCreator` class, you can use its API to create and drop tables (see "Creating Tables With a Table Creator").

## Using TopLink Workbench During Development

To create a `TableCreator` class that you can use in a Java application to recreate a database schema using the `SchemaManager`, use this procedure:

1. Right-click the project in the Navigator and choose Export > Table Creator Java Source from the context menu. The Table Creator dialog box appears.

   You can also select the table and choose Selected > Export > Table Creator Java Source from the menu.

2. Enter a name for the table creator class and click OK. The Save As dialog box appears.

3. Choose a location for your table creator class and click OK. TopLink Workbench exports the table creator Java class to the location you specify.

## Using the Default Table Generator at Run Time

To create a `TableCreator` class in Java using the `DefaultTableGenerator`, use this procedure:

1. Create an instance of `DefaultTableGenerator`, passing in an instance of your TopLink project:

       DefaultTableGenerator myDefTblGen = new DefaultTableGenerator(toplinkProject);

2. Create a `TableCreator` instance:

   - If you want a `TableCreator` that can support any session, use:

         TableCreator myTblCre = myDefTblGen.generateDefaultTableCreator();

   - If you want a `TableCreator` customized for a specific TopLink session, use:

         TableCreator myTblCre = myDefTblGen.generateFilteredDefaultTableCreator(toplinkSession);

You can also configure TopLink to use the `DefaultTableGenerator` to automatically generate and execute a `TableCreator` at run time (see "Automatic Database Table Creation").

## Using Java

This section describes how to create a `TableCreator` class in Java, including the following:

- Creating a TableCreator Class

- Creating a TableDefinition Class

- Adding Fields to a TableDefinition

- Defining Sybase and Microsoft SQL Server Native Sequencing

### Creating a TableCreator Class

To create your own `TableCreator` instance, you should extend `TableCreator` as Example 6-1 shows:

Example 6-1 Creating a TableCreator Class

    public class MyTableCreator extends oracle.toplink.tools.schemaframework.TableCreator {

        public M7TableCreator() {
            setName("MyTableCreator");
            addTableDefinition(buildADDRESSTable());
        ...
        }

        public TableDefinition buildADDRESSTable() {
            TableDefinition table = new TableDefinition();
           ...
           return table;
        }
    ...
    }

### Creating a TableDefinition Class

The `TableDefinition` class includes all the information required to create a new table, including the names and properties of a table and all its fields.

The `TableDefinition` class has the following methods:

- `setName`

- `addField`

- `addPrimaryKeyField`

- `addIdentityField`

- `addForeignKeyConstraint`

All table definitions must call the `setName` method to set the name of the table that is described by the `TableDefinition`.

### Adding Fields to a TableDefinition

Use the `addField` method to add fields to the `TableDefinition`. To add the primary key field to the table, use the `addPrimaryKeyField` method rather than the `addField` method.

To maintain compatibility among different databases, the type parameter requires a Java class rather than a database field type. TopLink translates the Java class to the appropriate database field type at run time. For example, the `String` class translates to the `CHAR` type for dBase databases. However, if you are connecting to Sybase, the `String` class translates to `VARCHAR`. For more information, see "Schema Manager Java and Database Type Conversion".

The `addField` method can also be called with the `fieldSize` or `fieldSubSize` parameters for column types that require size and subsize to be specified.

Some databases require a subsize, but others do not. TopLink automatically provides the required information, as necessary.

### Defining Sybase and Microsoft SQL Server Native Sequencing

Use `FieldDefinition` method `addIdentityField` to add fields representing a generated sequence number from Sybase or Microsoft SQL Server native sequencing. See "Native Sequencing With a Non-Oracle Database Platform" for detailed information on using sequencing.