Skip Headers
Oracle® Application Server TopLink Application Developer's Guide
10g Release 2 (10.1.2)
Part No. B15901-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
 

Schema Manager

The Schema Manager creates and modifies tables in a database from a Java application. As a Java code batch facility, the Schema Manager can also create sequence numbers on an existing database and generate stored procedures.

Use the Schema Manager to re-create a production database in a nonproduction environment. Doing this enables you to build models of your existing databases, and modify and test them during development.

Using the Schema Manager to Create Tables

The Schema Manager table creation mechanism uses Java types rather than database types, it is database-independent. However, this mechanism does not account for database specific optimizations. It is best-suited for development purposes rather than production.

The OracleAS TopLink TableDefinition class enables you to create new database table schemas in a generic format. At runtime, OracleAS TopLink determines the database type and uses the generic schemas to create the appropriate fields for that database.

Creating a Table Definition

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 Table Definition

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. OracleAS TopLink translates the Java class to the appropriate database field type at runtime. 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.

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. OracleAS TopLink automatically provides the required information, as necessary.

Defining Sybase and Microsoft SQL Server Native Sequencing

The addIdentityField() methods have the following definitions:

addIdentityField(String fieldName, Class type) 
addIdentityField(String fieldName, Class type, int fieldSize)

These methods enable you to add fields representing a generated sequence number from Sybase or Microsoft SQL Server native sequencing.

The OracleAS TopLink Two-Tier Example illustrates the table creation mechanism in the EmployeeTableCreator.java file located in the <ORACLE_HOME>\toplink\examples\foundation\twotier\src\examples\sessions\twotier\ directory.

Creating Tables on the Database

OracleAS TopLink offers two methods that enable you to pass the initialized TableDefinition object to the DatabaseSession Schema Manager:

  • The createObject() method creates a new table in the database, according to the table definition.

    SchemaManager schemaManager = new SchemaManager(session);
    schemaManager.createObject(Tables.employeeTable());
    
    
  • The replaceObject() method destroys and re-creates the schema entity in the database.

    SchemaManager schemaManager = new SchemaManager(session);
    schemaManager.replaceObject(Tables.addressTable());
    
    

Creating the Sequence Table

If your application requires a sequence table, invoke the createSequences() method on the Schema Manager:

SchemaManager schemaManager = new SchemaManager(session);
schemaManager.createSequences();

The preceding code:

  • Creates the sequence table as defined in the session DatabaseLogin

  • Creates or inserts sequences for each sequence name for all registered descriptors in the session

  • Creates the Oracle sequence object if you use Oracle native sequencing

Managing Java and Database Type Conversions

Table A-1 lists the field types that match a given class for each database that OracleAS TopLink supports. This list is specific to the Schema Manager and does not apply to mappings. OracleAS TopLink automatically performs conversions between any database types within mappings.

Table A-1 OracleAS TopLink Classes and Database Field Types

Class Oracle Type DB2 Type dBase Type Sybase Type Microsoft Access Type
java.lang.Boolean NUMBER SMALLINT NUMBER BIT default 0 SHORT
java.lang.Byte NUMBER SMALLINT NUMBER SMALLINT SHORT
java.lang.Byte[] LONG RAW BLOB BINARY IMAGE LONGBINARY
java.lang.Integer NUMBER INTEGER NUMBER INTEGER LONG
java.lang.Long NUMBER INTEGER NUMBER NUMERIC DOUBLE
java.lang.Float NUMBER FLOAT NUMBER FLOAT(16) DOUBLE
java.lang.Double NUMBER FLOAT NUMBER FLOAT(32) DOUBLE
java.lang.Short NUMBER SMALLINT NUMBER SMALLINT SHORT
java.lang.String VARCHAR2 VARCHAR CHAR VARCHAR TEXT
java.lang.Character CHAR CHAR CHAR CHAR TEXT
java.lang.Character[] LONG CLOB MEMO TEXT LONGTEXT
java.math.BigDecimal NUMBER DECIMAL NUMBER NUMERIC DOUBLE
java.math.BigInteger NUMBER DECIMAL NUMBER NUMERIC DOUBLE
java.sql.Date DATE DATE DATE DATETIME DATETIME
java.sql.Time DATE TIME CHAR DATETIME DATETIME
java.sql.Timestamp DATE TIMESTAMP CHAR DATETIME DATETIME