Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Using the Schema Manager (ELUG)"

m (New page: <div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__ Related Topics</div> The <tt>SchemaManager</tt> a...)
 
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Image:Elug draft icon.png]] '''For the latest EclipseLink documentation, please see http://www.eclipse.org/eclipselink/documentation/ '''
 +
 +
----
 +
 
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__
 
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__
 
[[Special:Whatlinkshere/Using the Schema Manager (ELUG)|Related Topics]]</div>
 
[[Special:Whatlinkshere/Using the Schema Manager (ELUG)|Related Topics]]</div>
 
The <tt>SchemaManager</tt> and its related classes provide API that you can use from a Java application to specify database tables in a generic format, and then create and modify them in a specific relational database. This decouples your EclipseLink project from a particular database schema while giving you a programmatic means of creating a database schema based on your EclipseLink project. For example, you can use the schema manager to recreate a production database in a nonproduction environment. This lets you build models of your existing databases, and modify and test them during development.
 
The <tt>SchemaManager</tt> and its related classes provide API that you can use from a Java application to specify database tables in a generic format, and then create and modify them in a specific relational database. This decouples your EclipseLink project from a particular database schema while giving you a programmatic means of creating a database schema based on your EclipseLink project. For example, you can use the schema manager to recreate a production database in a nonproduction environment. This lets you build models of your existing databases, and modify and test them during development.
 
 
  
 
{| class="Note oac_no_warn" width="80%" border="1" frame="hsides" rules="groups" cellpadding="3" frame="hsides" rules="groups"
 
{| class="Note oac_no_warn" width="80%" border="1" frame="hsides" rules="groups" cellpadding="3" frame="hsides" rules="groups"
 
| align="left" |
 
| align="left" |
'''Note:''' You can also create database tables manually during development using the Workbench (see [[Using%20Workbench%20(ELUG).|Creating New Tables]] and [[Using%20Workbench%20(ELUG)|Generating Tables on the Database]]).
+
'''Note:''' You can also create database tables manually during development using the Workbench (see [[Using%20Workbench%20(ELUG)#Creating New Tables|Creating New Tables]] and [[Using%20Workbench%20(ELUG)#Generating Tables on the Database|Generating Tables on the Database]]).
 
|}
 
|}
  
<br />
 
  
  
Line 25: Line 26:
  
 
You can automatically generate a <tt>TableCreator</tt> using the following:
 
You can automatically generate a <tt>TableCreator</tt> using the following:
 
+
* [[#How to Use Workbench During Development|Workbench during development]]
* Workbench during development (see [[#How to Use Workbench During Development|How to Use Workbench During Development]])
+
* [[#How to Use the Default Table Generator at Run Time|<tt>DefaultTableGenerator</tt> at run time]]
* <tt>DefaultTableGenerator</tt> at run time (see [[#How to Use the Default Table Generator at Run Time|How to Use the Default Table Generator at Run Time]])
+
  
 
The <tt>TableCreator</tt> class owns one or more <tt>TableDefinition</tt> classes (one for each database table) and the <tt>TableDefinition</tt> class owns one or more <tt>FieldDefinition</tt> classes (one for each field).
 
The <tt>TableCreator</tt> class owns one or more <tt>TableDefinition</tt> classes (one for each database table) and the <tt>TableDefinition</tt> class owns one or more <tt>FieldDefinition</tt> classes (one for each field).
Line 33: Line 33:
 
The <tt>TableDefinition</tt> class lets you specify a database table schema in a generic format. At run time, EclipseLink uses the session associated with your EclipseLink project to determine the specific database type, and uses the generic schema to create the appropriate tables and fields for that database.
 
The <tt>TableDefinition</tt> class lets you specify a database table schema in a generic format. At run time, EclipseLink uses the session associated with your EclipseLink project to determine the specific database type, and uses the generic schema to create the appropriate tables and fields for that database.
  
After creating a <tt>TableCreator</tt> class, you can use its API to create and drop tables (see [[#Creating Tables with a Table Creator]]). You can also configure EclipseLink to do this automatically (see [[#Creating Database Tables Automatically]]).
+
After creating a <tt>TableCreator</tt> class, you can use its API to create and drop tables (see [[#Creating Tables with a Table Creator|Creating Tables with a Table Creator]]). You can also configure EclipseLink to do this automatically (see [[#Creating Database Tables Automatically|Creating Database Tables Automatically]]).
  
Because the schema manager uses Java types rather than database types, it is database-independent. However, because it does not account for database-specific optimizations, it is best-suited for development purposes rather than production. For more information on how the schema manager maps Java types to database types, see [[#How to Use Schema Manager Java and Database Type Conversion]].
+
Because the schema manager uses Java types rather than database types, it is database-independent. However, because it does not account for database-specific optimizations, it is best-suited for development purposes rather than production. For more information on how the schema manager maps Java types to database types, see [[#How to Use Schema Manager Java and Database Type Conversion|How to Use Schema Manager Java and Database Type Conversion]].
  
Although the schema manager can handle the sequencing configuration that you specify in your EclipseLink project, there are some sequencing restrictions you should be aware of (see [[#How to Use Sequencing]]).
+
Although the schema manager can handle the sequencing configuration that you specify in your EclipseLink project, there are some sequencing restrictions you should be aware of (see [[#How to Use Sequencing|How to Use Sequencing]]).
  
  
Line 266: Line 266:
 
|}
 
|}
  
<br />
 
  
For more information about database platforms that EclipseLink supports, see [[Introduction%20to%20Data%20Access%20(ELUG)|Database Platforms]].
+
For more information about database platforms that EclipseLink supports, see [[Introduction%20to%20Data%20Access%20(ELUG)#Database Platforms|Database Platforms]].
  
  
  
 
===How to Use Sequencing===
 
===How to Use Sequencing===
 
+
If you generate a <tt>TableCreator</tt> class [[#How to Use Workbench During Development|using the Workbench]] or [[#How to Use the Default Table Generator at Run Time|<tt>DefaultTableGenerator</tt>]], then sequencing configuration is included in your <tt>TableCreator</tt> according to your EclipseLink project configuration. In this case, when you use <tt>TableCreator</tt> method <tt>createTables</tt>, it does the following:
If you generate a <tt>TableCreator</tt> class using the Workbench (see [[#How to Use Workbench During Development]]) or <tt>DefaultTableGenerator</tt> (see [[#How to Use the Default Table Generator at Run Time]]), then sequencing configuration is included in your <tt>TableCreator</tt> according to your EclipseLink project configuration. In this case, when you use <tt>TableCreator</tt> method <tt>createTables</tt>, it does the following:
+
 
+
 
* Creates the sequence table as defined in the session <tt>DatabaseLogin</tt>.
 
* Creates the sequence table as defined in the session <tt>DatabaseLogin</tt>.
 
* Creates or inserts sequences for each sequence name for all registered descriptors in the session.
 
* 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.
 
* Creates the Oracle sequence object if you use Oracle native sequencing.
  
You can use advanced API to handle special cases like Sybase or Microsoft SQL Server native sequencing (see [[#How to Use Java to Create a Table Creator]]).
+
You can use advanced API to handle special cases like Sybase or Microsoft SQL Server native sequencing (see [[#How to Use Java to Create a Table Creator|How to Use Java to Create a Table Creator]]).
  
For more information about sequencing, see [[Introduction%20to%20Relational%20Projects%20(ELUG)|Sequencing in Relational Projects]].
+
For more information about sequencing, see [[Introduction%20to%20Relational%20Projects%20(ELUG)#Sequencing in Relational Projects|Sequencing in Relational Projects]].
  
  
  
 
==Creating a Table Creator==
 
==Creating a Table Creator==
 
 
You can automatically generate a <tt>TableCreator</tt> using:
 
You can automatically generate a <tt>TableCreator</tt> using:
 +
* [[#How to Use Workbench During Development2|Workbench during development]]
 +
* [[#How to Use the Default Table Generator at Run Time2|<tt>DefaultTableGenerator</tt> at run time]]
  
* Workbench during development (see [[#How to Use Workbench During Development|How to Use Workbench During Development]])
+
After creating a <tt>TableCreator</tt> class, you can use its API to create and drop tables (see [[#Creating Tables with a Table Creator|Creating Tables with a Table Creator]]).
* <tt>DefaultTableGenerator</tt> at run time (see [[#How to Use the Default Table Generator at Run Time|How to Use the Default Table Generator at Run Time]])
+
 
+
After creating a <tt>TableCreator</tt> class, you can use its API to create and drop tables (see [[#Creating Tables with a Table Creator|Creating Tables with a Table Creator|Creating Tables with a Table Creator|Creating Tables with a Table Creator]]).
+
  
  
  
 +
<span id="How to Use Workbench During Development2"></span>
 
===How to Use Workbench During Development===
 
===How to Use Workbench During Development===
 
 
To create a <tt>TableCreator</tt> class that you can use in a Java application to recreate a database schema using the <tt>SchemaManager</tt>, use this procedure:
 
To create a <tt>TableCreator</tt> class that you can use in a Java application to recreate a database schema using the <tt>SchemaManager</tt>, 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.<br>You can also select the table and choose '''Selected''' > '''Export > Table Creator Java Source''' from the menu.
# 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.
#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'''. Workbench exports the table creator Java class to the location you specify.
 
# Choose a location for your table creator class and click '''OK'''. Workbench exports the table creator Java class to the location you specify.
  
  
  
 +
<span id="How to Use the Default Table Generator at Run Time2"></span>
 
===How to Use the Default Table Generator at Run Time===
 
===How to Use the Default Table Generator at Run Time===
 
 
To create a <tt>TableCreator</tt> class in Java using the <tt>DefaultTableGenerator</tt>, use this procedure:
 
To create a <tt>TableCreator</tt> class in Java using the <tt>DefaultTableGenerator</tt>, use this procedure:
 
+
# Create an instance of <tt>DefaultTableGenerator</tt>, passing in an instance of your EclipseLink project: <br><tt>DefaultTableGenerator myDefTblGen = new DefaultTableGenerator(eclipselinkProject);</tt>
# Create an instance of <tt>DefaultTableGenerator</tt>, passing in an instance of your EclipseLink project: <br><tt>DefaultTableGenerator myDefTblGen = new DefaultTableGenerator(toplinkProject);</tt>
+
 
# Create a <tt>TableCreator</tt> instance:
 
# Create a <tt>TableCreator</tt> instance:
 
#*If you want a <tt>TableCreator</tt> that can support any session, use: <br><tt>TableCreator myTblCre = myDefTblGen.generateDefaultTableCreator();</tt>
 
#*If you want a <tt>TableCreator</tt> that can support any session, use: <br><tt>TableCreator myTblCre = myDefTblGen.generateDefaultTableCreator();</tt>
#*If you want a <tt>TableCreator</tt> customized for a specific EclipseLink session, use: <br><tt>TableCreator myTblCre = myDefTblGen.generateFilteredDefaultTableCreator(toplinkSession);</tt>
+
#*If you want a <tt>TableCreator</tt> customized for a specific EclipseLink session, use: <br><tt>TableCreator myTblCre = myDefTblGen.generateFilteredDefaultTableCreator(eclipselinkSession);</tt>
  
 
You can also configure EclipseLink to use the <tt>DefaultTableGenerator</tt> to automatically generate and execute a <tt>TableCreator</tt> at run time (see [[#Creating Database Tables Automatically|Creating Database Tables Automatically]]).
 
You can also configure EclipseLink to use the <tt>DefaultTableGenerator</tt> to automatically generate and execute a <tt>TableCreator</tt> at run time (see [[#Creating Database Tables Automatically|Creating Database Tables Automatically]]).
Line 321: Line 314:
  
 
===How to Use Java to Create a Table Creator===
 
===How to Use Java to Create a Table Creator===
 
 
This section describes how to create a <tt>TableCreator</tt> class in Java, including the following:
 
This section describes how to create a <tt>TableCreator</tt> class in Java, including the following:
 
 
* [[#Creating a TableCreator Class|Creating a TableCreator Class]]
 
* [[#Creating a TableCreator Class|Creating a TableCreator Class]]
 
* [[#Creating a TableDefinition Class|Creating a TableDefinition Class]]
 
* [[#Creating a TableDefinition Class|Creating a TableDefinition Class]]
Line 332: Line 323:
  
 
====Creating a TableCreator Class====
 
====Creating a TableCreator Class====
 
 
To create your own <tt>TableCreator</tt> instance, you should extend <tt>TableCreator</tt>, as the following example shows:
 
To create your own <tt>TableCreator</tt> instance, you should extend <tt>TableCreator</tt>, as the following example shows:
 
  
 
<span id="Example 5-1"></span>
 
<span id="Example 5-1"></span>
 
'''''Creating a TableCreator Class'''''
 
'''''Creating a TableCreator Class'''''
  public class MyTableCreator extends oracle.toplink.tools.schemaframework.TableCreator {
+
<source lang="java">
 +
  public class MyTableCreator extends org.eclipse.persistence.schemaframework.TableCreator {
 
   
 
   
 
     public M7TableCreator() {
 
     public M7TableCreator() {
Line 353: Line 343:
 
  ...
 
  ...
 
  }
 
  }
 
+
</source>
  
  
 
====Creating a TableDefinition Class====
 
====Creating a TableDefinition Class====
 
 
The <tt>TableDefinition</tt> class includes all the information required to create a new table, including the names and properties of a table and all its fields.
 
The <tt>TableDefinition</tt> class includes all the information required to create a new table, including the names and properties of a table and all its fields.
  
 
The <tt>TableDefinition</tt> class has the following methods:
 
The <tt>TableDefinition</tt> class has the following methods:
 
 
* <tt>setName</tt>
 
* <tt>setName</tt>
 
* <tt>addField</tt>
 
* <tt>addField</tt>
Line 373: Line 361:
  
 
====Adding Fields to a TableDefinition====
 
====Adding Fields to a TableDefinition====
 
 
Use the <tt>addField</tt> method to add fields to the <tt>TableDefinition</tt>. To add the primary key field to the table, use the <tt>addPrimaryKeyField</tt> method rather than the <tt>addField</tt> method.
 
Use the <tt>addField</tt> method to add fields to the <tt>TableDefinition</tt>. To add the primary key field to the table, use the <tt>addPrimaryKeyField</tt> method rather than the <tt>addField</tt> method.
  
Line 381: Line 368:
  
 
Some databases require a subsize, but others do not. EclipseLink automatically provides the required information, as necessary.
 
Some databases require a subsize, but others do not. EclipseLink automatically provides the required information, as necessary.
 +
 +
  
 
====Defining Sybase and Microsoft SQL Server Native Sequencing====
 
====Defining Sybase and Microsoft SQL Server Native Sequencing====
 +
Use <tt>FieldDefinition</tt> method <tt>addIdentityField</tt> to add fields representing a generated sequence number from Sybase or Microsoft SQL Server native sequencing. See [[Introduction%20to%20Relational%20Projects%20(ELUG)#Native Sequencing with a Non-Oracle Database Platform|Native Sequencing with a Non-Oracle Database Platform]] for detailed information on using sequencing.
 +
  
Use <tt>FieldDefinition</tt> method <tt>addIdentityField</tt> to add fields representing a generated sequence number from Sybase or Microsoft SQL Server native sequencing. See [[Introduction%20to%20Relational%20Projects%20(ELUG)|Native Sequencing with a Non-Oracle Database Platform]] for detailed information on using sequencing.
 
  
 
==Creating Tables with a Table Creator==
 
==Creating Tables with a Table Creator==
 
 
After creating a <tt>TableCreator</tt> class (see [[#Creating a Table Creator|Creating a Table Creator]]), you can use its API to create and drop tables. The important <tt>TableCreator</tt> methods are the following (each method takes an instance of <tt>DatabaseSession</tt>):
 
After creating a <tt>TableCreator</tt> class (see [[#Creating a Table Creator|Creating a Table Creator]]), you can use its API to create and drop tables. The important <tt>TableCreator</tt> methods are the following (each method takes an instance of <tt>DatabaseSession</tt>):
 
 
* <tt>createTables</tt>–this method creates tables, adds constraints, and creates sequence tables and sequences (if sequence tables already exist, this method drops them and recreates them).
 
* <tt>createTables</tt>–this method creates tables, adds constraints, and creates sequence tables and sequences (if sequence tables already exist, this method drops them and recreates them).
 
* <tt>dropTables</tt>–his method drops all constraints and drops all tables (except sequence tables) that the <tt>TableCreator</tt> defines.
 
* <tt>dropTables</tt>–his method drops all constraints and drops all tables (except sequence tables) that the <tt>TableCreator</tt> defines.
Line 396: Line 384:
 
* <tt>replaceTables</tt>–this method drops and then creates all tables that the <tt>TableCreator</tt> defines.
 
* <tt>replaceTables</tt>–this method drops and then creates all tables that the <tt>TableCreator</tt> defines.
  
==Creating Database Tables Automatically==
 
  
You can configure EclipseLink to create database tables automatically in JPA projects:
 
  
* [[#Creating Database Tables Automatically in JPA Projects|Creating Database Tables Automatically in JPA Projects]]
+
==Creating Database Tables Automatically==
 +
You can configure EclipseLink to create database tables automatically in JPA projects.
  
  
  
 
===Creating Database Tables Automatically in JPA Projects===
 
===Creating Database Tables Automatically in JPA Projects===
 
 
Using EclipseLink JPA persistence unit properties that you can define in a <tt>persistence.xml</tt> file, you can configure schema generation
 
Using EclipseLink JPA persistence unit properties that you can define in a <tt>persistence.xml</tt> file, you can configure schema generation
  
For more information, see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)|Using EclipseLink JPA Extensions for Schema Generation]].
+
For more information, see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#Using EclipseLink JPA Extensions for Schema Generation|Using EclipseLink JPA Extensions for Schema Generation]].
  
  
Line 417: Line 403:
  
 
[[Category: EclipseLink User's Guide]]
 
[[Category: EclipseLink User's Guide]]
[[Category: Draft]]
+
[[Category: Release 1]]
 
[[Category: Concept]]
 
[[Category: Concept]]
 +
[[Category: Task]]

Latest revision as of 11:20, 18 July 2012

Elug draft icon.png For the latest EclipseLink documentation, please see http://www.eclipse.org/eclipselink/documentation/


The SchemaManager and its related classes provide API that you can use from a Java application to specify database tables in a generic format, and then create and modify them in a specific relational database. This decouples your EclipseLink project from a particular database schema while giving you a programmatic means of creating a database schema based on your EclipseLink project. For example, you can use the schema manager to recreate a production database in a nonproduction environment. This lets you build models of your existing databases, and modify and test them during development.

Note: You can also create database tables manually during development using the Workbench (see Creating New Tables and Generating Tables on the Database).


Introduction to the Schema Manager

The following figure summarizes the important SchemaManager classes and the primary means of using them.


SchemaManager Usage

SchemaManager Usage

Although you can use the SchemaManager API directly, we recommend that you create a TableCreator class and use its API (which, in turn, uses the SchemaManager).

You can automatically generate a TableCreator using the following:

The TableCreator class owns one or more TableDefinition classes (one for each database table) and the TableDefinition class owns one or more FieldDefinition classes (one for each field).

The TableDefinition class lets you specify a database table schema in a generic format. At run time, EclipseLink uses the session associated with your EclipseLink project to determine the specific database type, and uses the generic schema to create the appropriate tables and fields for that database.

After creating a TableCreator class, you can use its API to create and drop tables (see Creating Tables with a Table Creator). You can also configure EclipseLink to do this automatically (see Creating Database Tables Automatically).

Because the schema manager uses Java types rather than database types, it is database-independent. However, because it does not account for database-specific optimizations, it is best-suited for development purposes rather than production. For more information on how the schema manager maps Java types to database types, see How to Use Schema Manager Java and Database Type Conversion.

Although the schema manager can handle the sequencing configuration that you specify in your EclipseLink project, there are some sequencing restrictions you should be aware of (see How to Use Sequencing).


How to Use Schema Manager Java and Database Type Conversion

The following table lists the Java type to database type conversions that the schema manager supports depending on the database platform your EclipseLink project uses. This list is specific to the schema manager and does not apply to mappings. EclipseLink automatically performs conversions between any database types within mappings.


Java and Database Field Type Conversion

Java Type Oracle DB2 Sybase MySQL MS Access

java.lang.Boolean

NUMBER

SMALLINT

BIT default 0

TINYINT(1)

SHORT

java.lang.Byte

NUMBER

SMALLINT

SMALLINT

TINYINT

SHORT

java.lang.Byte[]

LONG RAW

BLOB

IMAGE

BLOB

LONGBINARY

java.lang.Character

CHAR

CHAR

CHAR

CHAR

TEXT

java.lang.Character[]

LONG

CLOB

TEXT

TEXT

LONGTEXT

java.lang.Double

NUMBER

FLOAT

FLOAT(32)

DOUBLE

DOUBLE

java.lang.Float

NUMBER

FLOAT

FLOAT(16)

FLOAT

DOUBLE

java.lang.Integer

NUMBER

INTEGER

INTEGER

INTEGER

LONG

java.lang.Long

NUMBER

INTEGER

NUMERIC

BIGINT

DOUBLE

java.lang.Short

NUMBER

SMALLINT

SMALLINT

SMALLINT

SHORT

java.lang.String

VARCHAR2

VARCHAR

VARCHAR

VARCHAR

TEXT

java.math.BigDecimal

NUMBER

DECIMAL

NUMERIC

DECIMAL

DOUBLE

java.math.BigInteger

NUMBER

DECIMAL

NUMERIC

BIGINT

DOUBLE

java.sql.Date

DATE

DATE

DATETIME

DATE

DATETIME

java.sql.Time

DATE

TIME

DATETIME

TIME

DATETIME

java.sql.Timestamp

DATE

TIMESTAMP

DATETIME

DATETIME

DATETIME


For more information about database platforms that EclipseLink supports, see Database Platforms.


How to Use Sequencing

If you generate a TableCreator class using the Workbench or DefaultTableGenerator, then sequencing configuration is included in your TableCreator according to your EclipseLink project configuration. In this case, when you use TableCreator method createTables, it does the following:

  • 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.

You can use advanced API to handle special cases like Sybase or Microsoft SQL Server native sequencing (see How to Use Java to Create a Table Creator).

For more information about sequencing, see Sequencing in Relational Projects.


Creating a Table Creator

You can automatically generate a TableCreator using:

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


How to Use 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. Workbench exports the table creator Java class to the location you specify.


How to Use 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 EclipseLink project:
    DefaultTableGenerator myDefTblGen = new DefaultTableGenerator(eclipselinkProject);
  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 EclipseLink session, use:
      TableCreator myTblCre = myDefTblGen.generateFilteredDefaultTableCreator(eclipselinkSession);

You can also configure EclipseLink to use the DefaultTableGenerator to automatically generate and execute a TableCreator at run time (see Creating Database Tables Automatically).


How to Use Java to Create a Table Creator

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


Creating a TableCreator Class

To create your own TableCreator instance, you should extend TableCreator, as the following example shows:

Creating a TableCreator Class

 public class MyTableCreator extends org.eclipse.persistence.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. EclipseLink 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 How to Use 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. EclipseLink 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.


Creating Tables with a Table Creator

After creating a TableCreator class (see Creating a Table Creator), you can use its API to create and drop tables. The important TableCreator methods are the following (each method takes an instance of DatabaseSession):

  • createTables–this method creates tables, adds constraints, and creates sequence tables and sequences (if sequence tables already exist, this method drops them and recreates them).
  • dropTables–his method drops all constraints and drops all tables (except sequence tables) that the TableCreator defines.
  • createConstraints–this method creates constraints on all pre-existing tables that the TableCreator defines.
  • dropConstraints–this method drops constraints on all pre-existing tables that the TableCreator defines.
  • replaceTables–this method drops and then creates all tables that the TableCreator defines.


Creating Database Tables Automatically

You can configure EclipseLink to create database tables automatically in JPA projects.


Creating Database Tables Automatically in JPA Projects

Using EclipseLink JPA persistence unit properties that you can define in a persistence.xml file, you can configure schema generation

For more information, see Using EclipseLink JPA Extensions for Schema Generation.



Copyright Statement

Back to the top