Oracle TopLink Developer's Guide
10g Release 3 (10.1.3) B13593-01 |
|
![]() Previous |
![]() Next |
In relational projects, when you expand the database object in the Navigator, TopLink Workbench displays the database tables associated with the project. You can associate tables by importing them from the database, or by creating them within TopLink Workbench.
Figure 5-15 numbered callouts identify the following database icons.
Project
Database
Database table
Each database table property sheet contains the following tabs in the Editor:
Columns – Add or modify the table's fields, and specify each field's properties.
References – Specify references between tables.
This section includes information on the following topics:
This section describes the following options:
See "Working With Database Tables in the Editor Window" for more information.
To log in or out of a relational database:
Create a database login (see "Database Login Configuration Overview").
To log in a relational database, right-click the database object in the Navigator, and choose Log In to Database from the context menu or choose Selected > Log In to Database from the menu.
To log out of a relational database, right-click the database object in the Navigator and choose Log Out of Database from the context menu or choose Selected > Log Out of Database from the menu.
To create a new database table within TopLink Workbench, use the following procedure:
Select the database object in the Navigator window and click Add New Table. The New Table dialog box appears.
You can also right-click the database object and choose Add New Table from the context menu, or choose Selected > Add New Table from the menu.
Complete each field on the New Table dialog box and click OK.
Field | Description |
---|---|
Catalog |
|
Table Name | Specify the name of this database table. |
Schema |
|
TopLink Workbench adds the database table to the project.
Although the database table has been added to the project, it has not been written to the actual database. See "Generating Tables on the Database" for more information on creating the table in the database.
Continue with "Working With Database Tables in the Editor Window" to use these tables in your project.
TopLink Workbench can automatically read the schema for a relational database and import the table data into the project as long as your JDBC driver supports the following JDBC methods:
The JDBC driver must be on the TopLink Workbench classpath (see "Configuring the TopLink Workbench Environment").
To import tables from the database, use the following procedure:
Select the database object in the Navigator, and click Add/Update Existing Tables from Database. The Import Tables from Database dialog box appears.
You can also right-click on the database object in the Navigator and choose Add/Update Existing Tables from Database from the context menu or choose Selected > Add/Update Existing Tables from Database from the menu.
Figure 5-17 Import Tables from Database Dialog Box
Figure 5-17 numbered callouts identify the following user interface components:
Filters
Database tables that match the filters
Complete each field on the Import Tables from Database dialog box and click OK.
Examine each table's properties to verify that the imported tables contain the correct information. See "Working With Database Tables in the Editor Window" for more information.
To remove a database table from the project, use the following procedure:
Select a database table in the Navigator, and click Remove Table on the toolbar. TopLink Workbench prompts for confirmation.
You can also right-click on the database object and choose Remove from the context menu or choose Selected > Remove Table from the menu.
Click OK. TopLink Workbench removes the table from the project.
Note: Although you have removed the table from the TopLink Workbench project, the table remains in the database. |
To rename a database table in the TopLink Workbench project, use the following procedure:
Right-click the table in the Navigator and choose Rename from the context menu. The Rename dialog box appears.
You can also select the table and choose Selected > Rename from the menu.
Enter a new name and click OK. TopLink Workbench renames the table.
Note: Although you have renamed the table in the TopLink Workbench project, the original table name remains in the database. |
To refresh (that is, reload) the database tables in the TopLink Workbench project, use this procedure:
Select a database table in the Navigator, and click Refresh from Database on the toolbar.
You can also select the table and choose Selected > Refresh from Database from the menu, or click Refresh. TopLink Workbench reloads the database table.
When refreshing tables from the database, if there are multiple database tables with similar names, the Duplicate Tables dialog box appears.
Select the specific database table to update, and then click OK.
When you select a database table in the Navigator, its properties appear in the Editor. Each database table contains the following property tabs:
Columns–Add or modify the table fields, and specify each field properties.
References–Specify references between tables.
This section describes how to use these tabs to configure the following:
Use the database table's Column tab to specify properties for the database table's fields.
To specify a table's column properties, use this procedure:
Select a database table in the Navigator. The table's property sheet displays in the Editor.
Click the Columns tab.
Enter data in each field on the Columns tab. Use the scroll bar to display the additional field.
Field | Description |
---|---|
Name | Specify the name of the field. |
Type | Use the drop-down list to select the field's type.
Note: The valid values will vary, depending on the database. |
Size | Specify the size of the field. |
Sub-Size | Specify the sub-size of the field. |
Allows Null | Specify if this field can be null. |
Unique | Specify whether the value must be unique within the table. |
Primary Key | Specify whether or not this field is a primary key for the table (see "Setting a Primary Key for Database Tables"). |
Identity | Use to indicate a Sybase, SQL Server or Informix identity field. |
Note: Some properties may be unavailable, depending on your database type. |
To add a new field, click Add.
To remove a field, select the field and click Remove.
To rename a field, select the field and click Rename.
To set a primary key(s) for a database table, use this procedure:
Note: TopLink Workbench can automatically import primary key information if supported by the JDBC driver. |
Select a database table in the Navigator. Its property sheet appears in the Editor.
Click the Fields tab.
Figure 5-20 Setting Primary Key for a Database Table
Select the Primary Key field(s) for the table.
References are table properties that contain the foreign key; they may or may not correspond to an actual constraint that exists on the database. TopLink Workbench uses these references when you define relationship mappings and multiple table associations.
When importing tables from the database, TopLink Workbench can automatically create references (if the driver supports this), or you can define references from the workbench. See "Importing Tables from a Database".
To create a new table reference, use this procedure:
Select a database table in the Navigator. The table's properties display in the Editor.
Click the References tab.
Figure 5-23 numbered callouts identify the following user interface components:
Table References area
Key Pairs area
In the References area, click Add. The New Reference dialog box appears.
Complete each field on the New Reference dialog box and click OK.
Continue with "Creating Field Associations".
For each table reference, you can specify one or more field associations that define how fields in the source table relate to fields in the target table. See "Creating Table References".
To create new field references, use this procedure:
Select a database table in the Navigator. The table's properties display in the Editor.
Click the References tab.
Figure 5-23 numbered callouts identify the following user interface components:
Table references area
Key pairs area
Select a table reference from the references area.
To create a new key pair, click Add in the key pairs area and complete each field in the key pairs area using the following information:
Field | Description |
---|---|
Table References Area |
|
Reference Name | Specify the name of this table reference |
Target Table | Specify the database table that is the target of this reference. |
On Table | Specify if the reference exists on the database. |
Key Pairs Area |
|
Source Field | Select the database field from the source table. |
Target Field | Select the database field from the target table. |
TopLink Workbench can automatically generate a variety of information from the database tables. This section describes the following:
Using the TopLink Workbench, you can generate SQL scripts that you can use to create tables in a relational database.
To automatically generate SQL scripts to create the tables in a project, use this procedure:
Select the database table(s) in the Navigator.
Right-click the table(s) and choose Generate Creation Script for > Selected Table or All Tables from the context menu. The SQL Creation Script dialog box appears.
You can also choose Selected > Generate Creation Script for > Selected Table or All Tables from the menu.
Copy the script and paste it into a file. You may need to edit the file to include additional SQL information that TopLink Workbench could not generate.
Note: If TopLink cannot determine how a particular table feature should be implemented in SQL, it generates a descriptive message in the script. |
TopLink Workbench can automatically generate Java class definitions, descriptor definitions, and associated mappings from the information in database tables. You can later edit the generated information if necessary.
For each table, TopLink Workbench does the following:
Creates a class definition and a descriptor definition.
Adds attributes to the class for each column in the table.
Automatically generates access methods, if specified.
Creates direct-to-field mappings for all direct (nonforeign key) fields in the table.
Creates relationship mappings (one-to-one and one-to-many) if there is sufficient foreign key information. You may be required to determine the exact mapping type.
Note: Class and attribute names are generated based on the table and column names. You can edit the class properties to change their names. |
To generate classes and descriptors from database tables, use the following procedure:
Select the database table(s) in the Navigator.
Right-click the table(s) and choose Generate Classes and Descriptors from > Selected Table or All Tables from the context menu.
You can also choose Selected > Generate Classes and Descriptors from > Selected Table or All Tables from the menu.
Click Yes. The Generate Classes and Descriptors dialog box appears.
Complete each field on the Generate Classes and Descriptors dialog box and click OK.
Use the following information to enter data in each field:
If the table contains foreign key fields that may represent relationship mappings, then the Choose Relationships to Generate dialog box appears.
If the table contains foreign key fields that may represent relationship mappings, then the Choose Relationships to Generate dialog box appears.
Select an entry from Potential Relationships and click the 1:1 Mapping or 1:M Mapping button, located between the Potential Relationships and Selected Relationships windows. See "Understanding Relational Mappings" for more information on mappings.
You can also specify whether the relationships are bidirectional. See "Configuring Bidirectional Relationship" for more information.
Click OK to automatically create the relationships.
The newly created descriptors appear in the Navigator of TopLink Workbench.
.Using TopLink Workbench, you can automatically generate EJB entities and descriptors for each database table, including the following:
One EJB descriptor that implements the <javax.ejb.EntityBean>
interface and four EJB 1.1 classes for each table
Bean relation attributes (CMP or BMP)
Java source for each class
EJB-compliant method stubs
Note: This option is available only for projects with CMP or BMP persistence. See "Configuring Persistence Type" for more information. |
To automatically generate EJB entities and descriptors for each database table, use this procedure:
Select the database table(s) in the Navigator.
Right-click the table(s) and choose Generate EJB Entities and Descriptors from > Selected Table or All Tables from the context menu. TopLink Workbench prompts you to save your project.
You can also choose Selected > Generate EJB Entities and Descriptors from > Selected Table or All Tables from the menu.
Click Yes to save your project before generating EJB entities. The Generate EJB Entity Classes and Descriptors dialog box appears.
Figure 5-27 Generate EJB Entity Classes and Descriptors Dialog Box
Complete each field on the Generate EJB Entity Classes and Descriptors dialog box and click OK.
Field | Description |
---|---|
Package Name | Name of the package to contain the generated entities and descriptors. |
Generate Local InterfacesFoot 1 | Specify if TopLink creates local interfaces for the EJB entities. |
Generate Remote InterfacesFootref 1 | Specify if TopLink creates remote interfaces for the EJB entities. |
Footnote 1 For CMP 2.0 and BMP projects only. See "Configuring a Descriptor With EJB Information" for more information.
If the table contains foreign key fields that may represent relationship mappings, then the Choose Relationships to Generate dialog box appears. Select a potential relationship and click the 1:1 Mapping or 1:M Mapping button, located between the Potential Relationships and Selected Relationships windows.
You can also specify if the relationships are bidirectional. See "Configuring Bidirectional Relationship" for more information.
Repeat for all appropriate sets of tables.
Click OK to generate the relationship mappings.
The system creates the remote primary key, home, and bean classes for each bean and adds this information to the project.
To create a table in the database, based on the information in TopLink Workbench, use this procedure:
Note: You must log in the database before creating tables. See "Logging in to the Database" for more information. |
Select the database table(s) in the Navigator.
Right-click the table(s) and choose Create on Database > Selected Table or All Tables from the context menu.
You can also create tables by selecting Selected > Create on Database > Selected Table or All Tables from the menu.
TopLink Workbench creates the tables on the database.
Alternatively, you can generate tables at run time by exporting the information in TopLink Workbench to a TableCreator
class (see "Understanding the Schema Manager").