Datasource Customization


JBoss includes predefined type-mappings for many databases, including Cloudscape, DB2, DB2/400, Hypersonic SQL, InformixDB, InterBase, Microsoft SQL Server, Microsoft SQL Server 2000, MySQL, Oracle7, Oracle8, Oracle9i, PointBase, PostgreSQL, PostgreSQL 7.2, SapDB, SOLID, and Sybase. If you do not like a supplied mapping, or if a mapping is not supplied for your database, you have to define a new mapping.

Type Mapping

You customize a database through the type-mapping section of the jbosscmp-jdbc.xml descriptor. The content model for the type-mapping element is shown in Figure 11.17.

Figure 11.17. The jbosscmp-jdbc type-mapping element content model.


The elements of the type-mapping element are as follows:

  • name This required element provides a name that identifies the database customization. You use it to refer to the mappings by the datasource-mapping elements found in defaults and entity.

  • row-locking-template This required element gives the PreparedStatement template used to create a row lock on the selected rows. The template must support three arguments:

    • The select clause.

    • The from clause. The order of the tables is currently not guaranteed.

    • The where clause.

    If row locking is not supported in a select statement, this element should be empty. The most common form of row locking is select for update, as in SELECT ?1 FROM ?2 WHERE ?3 FOR UPDATE.

  • pk-constraint-template This required element specifies the PreparedStatement template that is used to create a primary key constraint in the create table statement. The template must support two arguments:

    • The primary key constraint name (which is always pk_{table-name}

    • A comma-separated list of primary key column names

    If a primary key constraint clause is not supported in a create table statement, this element should be empty. The most common form of a primary key constraint is CONSTRAINT ?1 PRIMARY KEY (?2).

  • fk-constraint-template This is the template that is used to create a foreign key constraint in a separate statement. The template must support five arguments:

    • 1 is the table name.

    • 2 is the foreign key constraint name, which is always fk_{table-name}_{cmr-field-name}.

    • 3 is the comma-separated list of foreign key column names.

    • 4 is the references table name.

    • 5 is the comma-separated list of the referenced primary key column names.

    If the datasource does not support foreign key constraints, this element should be empty. The most common form of a foreign key constraint is ALTER TABLE ?1 ADD CONSTRAINT ?2 FOREIGN KEY (?3) REFERENCES ?4 (?5).

  • auto-increment-template This declares the SQL template for specifying autoincrement columns.

  • add-column-template When alter-table is true, this SQL template specifies the syntax for adding a column to an existing table. The default value is ALTER TABLE ?1 ADD ?2 ?3. The parameters are as follows:

    • 1 is the table name.

    • 2 is the column name.

    • 3 is the column type.

  • alter-column-template When alter-table is TRue, this SQL template specifies the syntax for dropping a column from an existing table. The default value is ALTER TABLE ?1 ALTER ?2 TYPE ?3. The parameters are as follows:

    • 1 is the table name.

    • 2 is the column name.

    • 3 is the column type.

  • drop-column-template When alter-table is TRue, this SQL template specifies the syntax for dropping a column from an existing table. The default value is ALTER TABLE ?1 DROP ?2. The parameters are as follows:

    • 1 is the table name.

    • 2 is the column name.

  • alias-header-prefix This required element gives the prefix used in creating the alias header. The ELB-QL compiler prepends an alias header to a generated table alias to prevent name collisions. The alias header is constructed as follows: alias-header-prefix + int_counter + alias-header-suffix. An example of an alias header would be t0_ for an alias-header-prefix of t and an alias-header-suffix of _.

  • alias-header-suffix This required element gives the suffix portion of the generated alias header.

  • alias-max-length This required element gives the maximum allowed length for the generated alias header.

  • subquery-supported This required element specifies whether subqueries are supported. Some EJB-QL operators are mapped to exists subqueries. If subquery-supported is false, the EJB-QL compiler uses left-join and is null.

  • true-mapping This required element defines a true identity in EJB-QL queries. Examples include TRUE, 1, and (1=1).

  • false-mapping This required element defines a false identity in EJB-QL queries. Examples include FALSE, 0, and (1=0).

  • function-mapping This optional element specifies one or more mappings from an EJB-QL function to an SQL implementation. The following section provides details.

  • mapping This required element specifies the mappings from a Java type to the corresponding JDBC and SQL types. See the section "Mapping," later in this chapter, for details.

Function Mapping

The function-mapping element content model is shown in Figure 11.18.

Figure 11.18. The jbosscmp-jdbc function-mapping element content model.


The allowed child elements are as follows:

  • function-name This required element gives the EJB-QL function name (for example, concat, substring).

  • function-sql This required element gives the SQL for the function, as appropriate for the underlying database. Examples for a concat function are (?1 || ?2), concat (?1, ?2), and (?1 +?2).

Mapping

A type mapping is simply a set of mappings between Java class types and database types. A set of type mappings is defined by a set of mapping elements, the content model for which is shown in Figure 11.19.

Figure 11.19. The jbosscmp-jdbc mapping element content model.


If JBoss cannot find a mapping for a type, it serializes the object and uses the java.lang.Object mapping. The following are the child elements of the mapping element:

  • java-type This required element gives the fully qualified name of the Java class to be mapped. If the class is a primitive wrapper class, such as java.lang.Short, the mapping also applies to the primitive type.

  • jdbc-type This required element gives the JDBC type that is used when setting parameters in a JDBC PreparedStatement or loading data from a JDBC ResultSet. The valid types are defined in java.sql.Types.

  • sql-type This required element gives the SQL type that is used in create table statements. Valid types are limited only by your database vendor.

  • param-setter This optional element specifies the fully qualified name of the JDBCParameterSetter implementation for this mapping.

  • result-reader This optional element specifies the fully qualified name of the JDBCResultSetReader implementation for this mapping.

The following is an example of a mapping element for a short in Oracle9i:

 <jbosscmp-jdbc>     <type-mappings>         <type-mapping>             <name>Oracle9i</name>             <!--...-->             <mapping>                 <java-type>java.lang.Short</java-type>                 <jdbc-type>NUMERIC</jdbc-type>                 <sql-type>NUMBER(5)</sql-type>             </mapping>         </type-mapping>     </type-mappings> </jbosscmp-jdbc> 

User Type Mappings

User type mappings allow you to map from JDBC column types to custom CMP field types by specifying an instance of an org.jboss.ejb.plugins.cmp.jdbc.Mapper interface, the definition of which is shown here:

 public interface Mapper {     /**      * This method is called when CMP field is stored.      * @param fieldValue - CMP field value      * @return column value.      */     Object toColumnValue(Object fieldValue);     /**      * This method is called when CMP field is loaded.      * @param columnValue - loaded column value.      * @return CMP field value.      */     Object toFieldValue(Object columnValue); } 

A prototypical use case is the mapping of an integer type to its type-safe Java enumeration instance. The content model of the user-type-mappings element consists of one or more user-type-mapping elements, the content model of which is shown in Figure 11.20.

Figure 11.20. The user-type-mapping content model.


The user-type-mapping elements are as follows:

  • java-type The fully qualified name of the CMP field type in the mapping.

  • mapped-type The fully qualified name of the database type in the mapping.

  • mapper The fully qualified name of the Mapper interface implementation that handles the conversion between java-type and mapped-type.



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net