Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Developer's Guide

- Previous: Container-Managed Persistence Mapping
- Next: Schema Capture

# Automatic Schema Generation

The automatic schema generation feature provided in the Application Server defines database tables based on the fields in entity beans and the relationships between the fields. This insulates developers from many of the database related aspects of development, allowing them to focus on entity bean development. The resulting schema is usable as-is or can be given to a database administrator for tuning with respect to performance, security, and so on.

This section addresses the following topics:

- Supported Data Types

- Generation Options

## Supported Data Types

CMP supports a set of JDBC data types that are used in mapping Java data fields to SQL types. Supported JDBC data types are as follows: BIGINT, BIT, BLOB, CHAR, CLOB, DATE, DECIMAL, DOUBLE, FLOAT, INTEGER, NUMERIC, REAL, SMALLINT, TIME, TIMESTAMP, TINYINT, VARCHAR.

The following table contains the mappings of Java types to JDBC types when automatic mapping is used.

Table 7–1 Java Type to JDBC Type Mappings

Java Type | JDBC Type | Nullability
--- | --- | ---
`boolean` | `BIT` | No
`java.lang.Boolean` | `BIT` | Yes
`byte` | `TINYINT` | No
`java.lang.Byte` | `TINYINT` | Yes
`double` | `DOUBLE` | No
`java.lang.Double` | `DOUBLE` | Yes
`float` | `REAL` | No
`java.lang.Float` | `REAL` | Yes
`int` | `INTEGER` | No
`java.lang.Integer` | `INTEGER` | Yes
`long` | `BIGINT` | No
`java.lang.Long` | `BIGINT` | Yes
`short` | `SMALLINT` | No
`java.lang.Short` | `SMALLINT` | Yes
`java.math.BigDecimal` | `DECIMAL` | Yes
`java.math.BigInteger` | `DECIMAL` | Yes
`char` | `CHAR` | No
`java.lang.Character` | `CHAR` | Yes
`java.lang.String` | `VARCHAR` or `CLOB` | Yes
`Serializable` | `BLOB` | Yes
`byte[]` | `BLOB` | Yes
`java.util.Date` | `DATE` (Oracle only) `TIMESTAMP` (all other databases) | Yes
`java.sql.Date` | `DATE` | Yes
`java.sql.Time` | `TIME` | Yes
`java.sql.Timestamp` | `TIMESTAMP` | Yes

Note –

Java types assigned to CMP fields must be restricted to Java primitive types, Java `Serializable` types, `java.util.Date`, `java.sql.Date`, `java.sql.Time`, or `java.sql.Timestamp`. An entity bean local interface type (or a collection of such) can be the type of a CMR field.

The following table contains the mappings of JDBC types to database vendor-specific types when automatic mapping is used. For a list of the JDBC drivers currently supported by the Application Server, see the Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Release Notes. For configurations of supported and other drivers, see Configurations for Specific JDBC Drivers.

Table 7–2 Mappings of JDBC Types to Database Vendor Specific Types

JDBC Type | PointBase | Oracle | DB2 | Sybase ASE 12.5 | MS-SQL Server
--- | --- | --- | --- | --- | ---
`BIT` | `BOOLEAN` | `SMALLINT` | `SMALLINT` | `TINYINT` | `BIT`
`TINYINT` | `SMALLINT` | `SMALLINT` | `SMALLINT` | `TINYINT` | `TINYINT`
`SMALLINT` | `SMALLINT` | `SMALLINT` | `SMALLINT` | `SMALLINT` | `SMALLINT`
`INTEGER` | `INTEGER` | `INTEGER` | `INTEGER` | `INTEGER` | `INTEGER`
`BIGINT` | `BIGINT` | `NUMBER` | `BIGINT` | `NUMERIC` | `NUMERIC`
`REAL` | `FLOAT` | `REAL` | `FLOAT` | `FLOAT` | `REAL`
`DOUBLE` | `DOUBLE PRECISION` | `DOUBLE PRECISION` | `DOUBLE` | `DOUBLE PRECISION` | `FLOAT`
`DECIMAL(p,s)` | `DECIMAL(p,s)` | `NUMBER(p,s)` | `DECIMAL(p,s)` | `DECIMAL(p,s)` | `DECIMAL(p,s)`
`VARCHAR` | `VARCHAR` | `VARCHAR2` | `VARCHAR` | `VARCHAR` | `VARCHAR`
`DATE` | `DATE` | `DATE` | `DATE` | `DATETIME` | `DATETIME`
`TIME` | `TIME` | `DATE` | `TIME` | `DATETIME` | `DATETIME`
`TIMESTAMP` | `TIMESTAMP` | `TIMESTAMP(9)` | `TIMESTAMP` | `DATETIME` | `DATETIME`
`BLOB` | `BLOB` | `BLOB` | `BLOB` | `IMAGE` | `IMAGE`
`CLOB` | `CLOB` | `CLOB` | `CLOB` | `TEXT` | `NTEXT`

## Generation Options

Deployment descriptor elements or `asadmin` command line options can control automatic schema generation by:

- Creating tables during deployment

- Dropping tables during undeployment

- Dropping and creating tables during redeployment

- Specifying the database vendor

- Specifying that table names are unique

- Specifying type mappings for individual CMP fields

Note –

Before using these options, make sure you have a properly configured CMP resource. See Configuring the CMP Resource.

You can also use the deploytool to perform automatic mapping. For more information about using the deploytool, see the “Create Database Mapping” topic in the deploytool’s online help.

For a read-only bean, do not create the database schema during deployment. Instead, work with your database administrator to populate the data into the tables. See Using Read-Only Beans.

Automatic schema generation is not supported for beans with version column consistency checking. Instead, work with your database administrator to create the schema and add the required triggers. See Version Column Consistency Checking.

The following optional data subelements of the `cmp-resource` element in the `sun-ejb-jar.xml` file control the automatic creation of database tables at deployment. For more information about the `cmp-resource` element, see cmp-resource and Configuring the CMP Resource.

Table 7–3 `sun-ejb-jar.xml` Generation Elements

Element | Default | Description
--- | --- | ---
create-tables-at-deploy | `false` | If `true`, causes database tables to be created for beans that are automatically mapped by the EJB container. If `false`, does not create tables.
drop-tables-at-undeploy | `false` | If `true`, causes database tables that were automatically created when the bean(s) were last deployed to be dropped when the bean(s) are undeployed. If `false`, does not drop tables.
database-vendor-name | none | Specifies the name of the database vendor for which tables are created. Allowed values are `db2`, `mssql`, `oracle`, `pointbase`, and `sybase`, case-insensitive. If no value is specified, a connection is made to the resource specified by the `jndi-name` subelement of the `cmp-resource` element in the `sun-ejb-jar.xml` file, and the database vendor name is read. If the connection cannot be established, or if the value is not recognized, SQL-92 compliance is presumed.
schema-generator-properties | none | Specifies field-specific column attributes in `property` subelements. Each property name is of the following format: bean-name`.`field-name`.`attribute For example:
    Employee.firstName.jdbc-type
Column attributes are described in Table A–95. Also allows you to set the `use-unique-table-names` property. If `true`, this property specifies that generated table names are unique within each application server domain. The default is `false`. For further information and an example, see schema-generator-properties.

The following options of the `asadmin deploy` or `asadmin deploydir` command control the automatic creation of database tables at deployment:

Table 7–4 `asadmin deploy` and `asadmin deploydir` Generation Options

Option | Default | Description
--- | --- | ---
`--createtables` | none | If `true`, causes database tables to be created for beans that need them. If `false`, does not create tables. If not specified, the value of the `create-tables-at-deploy` attribute in `sun-ejb-jar.xml` is used.
`--dropandcreatetables` | none | If `true`, and if tables were automatically created when this application was last deployed, tables from the earlier deployment are dropped and fresh ones are created. If `true`, and if tables were not automatically created when this application was last deployed, no attempt is made to drop any tables. If tables with the same names as those that would have been automatically created are found, the deployment proceeds, but a warning indicates that tables could not be created. If `false`, settings of `create-tables-at-deploy` or `drop-tables-at-undeploy` in the `sun-ejb-jar.xml` file are overridden.
`--uniquetablenames` | none | If `true`, specifies that table names are unique within each application server domain. If not specified, the value of the `use-unique-table-names` property in `sun-ejb-jar.xml` is used.
`--dbvendorname` | none | Specifies the name of the database vendor for which tables are created. Allowed values are `db2`, `mssql`, `oracle`, `pointbase`, and `sybase`, case-insensitive. If not specified, the value of the `database-vendor-name` attribute in `sun-ejb-jar.xml` is used. If no value is specified, a connection is made to the resource specified by the `jndi-name` subelement of the `cmp-resource` element in the `sun-ejb-jar.xml` file, and the database vendor name is read. If the connection cannot be established, or if the value is not recognized, SQL-92 compliance is presumed.

If one or more of the beans in the module are manually mapped and you use any of the `asadmin deploy` or `asadmin deploydir` options, the deployment is not harmed in any way, but the options have no effect, and a warning is written to the server log.

If the deploytool mapped one or more of the beans, the `--uniquetablenames` option of `asadmin deploy` or `asadmin deploydir` has no effect. The uniqueness of the table names was established when deploytool created the mapping.

The following options of the `asadmin undeploy` command control the automatic removal of database tables at undeployment:

Table 7–5 `asadmin undeploy` Generation Options

Option | Default | Description
--- | --- | ---
`--droptables` | none | If `true`, causes database tables that were automatically created when the bean(s) were last deployed to be dropped when the bean(s) are undeployed. If `false`, does not drop tables. If not specified, the value of the `drop-tables-at-undeploy` attribute in `sun-ejb-jar.xml` is used.

For more information about the `asadmin deploy`, `asadmin deploydir`, and `asadmin undeploy` commands, see the Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Reference Manual.

When command line and `sun-ejb-jar.xml` options are both specified, the `asadmin` options take precedence.

- Previous: Container-Managed Persistence Mapping
- Next: Schema Capture

- © 2010, Oracle Corporation and/or its affiliates