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

Setting Up Database Sessions

A database session in OracleAS TopLink represents an application's dialog with a relational database. The DatabaseSession class keeps track of the following information:

An application uses the session to log in to the database and perform read and write operations on the objects stored therein. The session's lifetime is normally the same as the lifetime of the application.

OracleAS TopLink includes a test class so that you can test the descriptor mappings that you have created for this introductory tutorial. This class, Demo, among other things, tests the validity of the descriptors and logs into the database.

Logging Into a Database

To log into a database, an application must first read the project file into a Project instance. The Project creates the DatabaseSession and connects through login. The OracleAS TopLink Mapping Workbench can generate the project file (Employee.xml in this example), including the login information. The following code fragment illustrates this approach.

Example B-2 Logging in and Creating a Project Example Code

The following code example illustrates creating the EMPLOYEE project.

...
    import oracle.toplink.sessions.*;
    ...
    Project builderProject = oracle.toplink.tools.workbench.XMLProjectReader.read(ÒC:\\oracle\\toplink\\ tutorials\\intro\\Employee.xmlÓ);
    DatabaseSession session = builderProject.createDatabaseSession();
    session.login(); // or, session.login(userName, password);
    ...

See the loginToDatabase() method, in the Demo class, for a complete method.

Creating the Tables in Code

You can use OracleAS TopLink Mapping Workbench to create database tables. OracleAS TopLink can also create tables using the SchemaManager class. To use this method of creating tables, you must have already obtained a valid login.

The following example illustrates how to create the EMPLOYEE table after having logged into the database. The method createTables() on the Demo class contains sample code that uses the schema manager to create all the required tables for the introductory tutorial.

Example B-3 Creating Tables

The following code example illustrates creating the EMPLOYEE table.

import oracle.toplink.tools.schemaframework.*;
    import java.math.*;
    
    // Create table definition which supplies information about the table to be created.
    TableDefinition employeeTable = new TableDefinition();
    employeeTable.setName("EMPLOYEE");
    employeeTable.addIdentityField("EMP_ID", BigDecimal.class, 15);
    employeeTable.addField("NAME", String.class, 40);
    employeeTable.addField("ADDRESS_ID", BigDecimal.class, 15);
    
    // Create the table in the database.
    SchemaManager schemaManager = new SchemaManager(session);
    schemaManager.replaceObject(employeeTable); 
    
    // Create an empty table named SEQUENCE if it is not already there. This is used to hold the sequence number information such as name and counter.
    schemaManager.createSequences();