Oracle TopLink Developer's Guide
10g Release 3 (10.1.3) B13593-01 |
|
![]() Previous |
![]() Next |
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").
To create a TableCreator
class that you can use in a Java application to recreate a database schema using the SchemaManager
, use this procedure:
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.
Enter a name for the table creator class and click OK. The Save As dialog box appears.
Choose a location for your table creator class and click OK. TopLink Workbench exports the table creator Java class to the location you specify.
To create a TableCreator
class in Java using the DefaultTableGenerator
, use this procedure:
Create an instance of DefaultTableGenerator
, passing in an instance of your TopLink project:
DefaultTableGenerator myDefTblGen = new DefaultTableGenerator(toplinkProject);
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").
This section describes how to create a TableCreator
class in Java, including the following:
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; } ... }
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
.
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.
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.