Skip Headers

# Using Java

Previous

Next

To create a project using Java code, use this procedure:

1. Implement a project class that extends the `oracle.toplink.sessions.Project` class (see Example: Specifying a TopLink Project in Code).

2. Compile the project class.

Specifying a TopLink Project in Code

    /**
    * The class EmployeeProject is an example of an Oracle TopLink project defined in Java code. The individual parts of the project - the Login and the descriptors, are built inside of methods that are called by the constructor. Note that EmployeeProject extends the class oracle.toplink.sessions.Project
    */
    public class EmployeeProject extends oracle.toplink.sessions.Project {

    /**
    * Supply a zero-argument constructor that initializes all aspects of the project. Make sure that the login and all the descriptors are initialized and added to the project. Project-level properties, such as the name of the project, should be specified here
    */
    public EmployeeProject() {
      setName("EmployeeProject");
      applyLogin();

      addDescriptor(buildAddressDescriptor());
      addDescriptor(buildEmployeeDescriptor());
      addDescriptor(buildPhoneNumberDescriptor());
    }

    // Data source information
    public void applyLogin() {
      DatabaseLogin login = new DatabaseLogin();

      // use platform appropriate for underlying database
      login.usePlatform(
        new oracle.toplink.platform.database.oracle.Oracle9Platform());
      login.setDriverClassName("oracle.jdbc.OracleDriver");
      login.setConnectionString("jdbc:oracle:thin:@HOST:PORT:SID");
      login.setUserName("USER NAME");
      login.setEncryptedPassword("PASSWORD, ENCRYPTED");

      // Configuration Properties
        setDatasourceLogin(login);
    }

    /**
    * Descriptors are built by defining table info, setting properties (caching, etc.) and by adding mappings to the descriptor
    */

    // SECTION: DESCRIPTOR
    public ClassDescriptor buildAddressDescriptor() {
      RelationalDescriptor descriptor = new RelationalDescriptor();

      // specify the class to be made persistent
      descriptor.setJavaClass(examples.servletjsp.model.Address.class);

      // specify the tables to be used and primary key
      descriptor.addTableName("ADDRESS");
      descriptor.addPrimaryKeyFieldName("ADDRESS.ADDRESS_ID");

      // Descriptor Properties
      descriptor.useSoftCacheWeakIdentityMap();
      descriptor.setIdentityMapSize(100)
      descriptor.useRemoteSoftCacheWeakIdentityMap()
      descriptor.setRemoteIdentityMapSize(100)
      descriptor.setSequenceNumberFieldName("ADDRESS.ADDRESS_ID")
      descriptor.setSequenceNumberName("ADD_SEQ");
      descriptor.setAlias("Address");

      // Mappings
      DirectToFieldMapping cityMapping = new DirectToFieldMapping();
      cityMapping.setAttributeName("city");
      cityMapping.setFieldName("ADDRESS.CITY");
      descriptor.addMapping(cityMapping);

      // … Additional mappings are added to the descriptor using the addMapping() method

    return descriptor;}

Note: Use TopLink Workbench to create a Java project class from an existing project. This provides a starting point for a custom project class. For more information, see "Exporting Project Java Source".