web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Chapter 18 18Generating Entity Bean Chapter Primary Keys Each entity bean must have a unique primary key that is used to identify the bean instance. The primary key can be represented by a Java class, which must be a legal value type in RMI-IIOP. Therefore, it extends the java.io.Serializable interface. It must also provide an implementation of the Object.equals(Object other) and Object.hashCode() methods. Normally, the primary key fields of entity beans must be set in the ejbCreate() method. The fields are then used to insert a new record into the database. This can be a difficult procedure, however, bloating the method, and many databases now have built-in mechanisms for providing appropriate primary key values. A more elegant means of generating primary keys is for the user to implement a separate class that generates primary keys. This class can also implement database-specific programming logic for generating primary keys. You may either generate primary keys by hand, use a custom class, or allow the container to use the database tools to perform this for you. If you use a custom class, implement the com.borland.ejb.pm.PrimaryKeyGenerationListener interface, discussed in “Generating primary keys from a custom class” on page 164. To use the database tools, you can set properties for the CMP engine to generate primary keys depending upon the database vendor. See “Implementing primary key generation by the CMP engine” on page 164. Generating primary keys from a user class With enterprise beans, the primary key is represented by a Java class containing the unique data. This primary key class can be any class as long as that class is a legal value type in RMI-IIOP, meaning it extends the java.io.Serializable interface. It must also provide an implementation of the Object.equals(Object other) and Object.hashCode() methods, two methods which all Java classes inherit by definition. Chapter 18: Generating Entity Bean Primary Keys 163

Generating primary keys from a custom class Generating primary keys from a custom class Note To generate primary keys from a custom class, you must write a class that implements the com.borland.ejb.pm.PrimaryKeyGenerationListener interface. this is a new interface for generating primary keys. In previous versions of Borland Enterprise Server, this class was com.inprise.ejb.cmp.PrimaryKeyGenerator. This interface is still supported, but Borland recommends using the newer interface when possible. Next, you must inform the container of your intention to use your custom class to generate primary keys for your entity beans. To do this, you set a table property primaryKeyGenerationListener to the class name of your primary key generator. Implementing primary key generation by the CMP engine Primary key generation can also be implemented by the CMP engine. Borland provides four properties to support primary key generation using database specific features. These properties are: ■ getPrimaryKeyBeforeInsertSql ■ getPrimaryKeyAfterInsertSql ■ ignoreOnInsert ■ useGetGeneratedKeys All of these properties are table properties except ignoreOnInsert, which is a column property. Oracle Sequences: using getPrimaryKeyBeforeInsertSql The property getPrimaryKeyBeforeInsertSql is typically used in conjunction with Oracle Sequences. The value of this property is a SQL statement used to select a primary key generated from a sequence. For example, the property could be set to: SELECT MySequence.NEXTVAL FROM DUAL The CMP engine would execute this SQL and then extract the appropriate value from the ResultSet. This value will then be used as the primary key when performing the subsequent INSERT. The extraction from the ResultSet is based on the primary key's type SQL Server: using getPrimaryKeyAfterInsertSql and ignoreOnInsert Two properties need to be specified for cases involving SQL Server. The getPrimaryKeyAfterInsertSql property specified the SQL to execute after the INSERT has been performed. As above, the CMP engine extracts the primary key from the ResultSet based on the primary key's type. The property ignoreOnInsert must also be set to the name of the identity column. The CMP engine will then know not to set that column in the INSERT. 164 BES Developer’s Guide

Generating primary keys from a custom class<br />

Generating primary keys from a custom class<br />

Note<br />

To generate primary keys from a custom class, you must write a class that implements<br />

the com.borland.ejb.pm.PrimaryKeyGenerationListener interface.<br />

this is a new interface for generating primary keys. In previous versions of <strong>Borland</strong><br />

Enterprise Server, this class was com.inprise.ejb.cmp.PrimaryKeyGenerator. This<br />

interface is still supported, but <strong>Borland</strong> recommends using the newer interface when<br />

possible.<br />

Next, you must inform the container of your intention to use your custom class to<br />

generate primary keys for your entity beans. To do this, you set a table property<br />

primaryKeyGenerationListener to the class name of your primary key generator.<br />

Implementing primary key generation by the CMP engine<br />

Primary key generation can also be implemented by the CMP engine. <strong>Borland</strong> provides<br />

four properties to support primary key generation using database specific features.<br />

These properties are:<br />

■<br />

getPrimaryKeyBeforeInsertSql<br />

■<br />

getPrimaryKeyAfterInsertSql<br />

■<br />

ignoreOnInsert<br />

■<br />

useGetGeneratedKeys<br />

All of these properties are table properties except ignoreOnInsert, which is a column<br />

property.<br />

Oracle Sequences: using getPrimaryKeyBeforeInsertSql<br />

The property getPrimaryKeyBeforeInsertSql is typically used in conjunction with Oracle<br />

Sequences. The value of this property is a SQL statement used to select a primary key<br />

generated from a sequence. For example, the property could be set to:<br />

SELECT MySequence.NEXTVAL FROM DUAL<br />

The CMP engine would execute this SQL and then extract the appropriate value from<br />

the ResultSet. This value will then be used as the primary key when performing the<br />

subsequent INSERT. The extraction from the ResultSet is based on the primary key's<br />

type<br />

SQL Server: using getPrimaryKeyAfterInsertSql and<br />

ignoreOnInsert<br />

Two properties need to be specified for cases involving SQL Server. The<br />

getPrimaryKeyAfterInsertSql property specified the SQL to execute after the INSERT has<br />

been performed. As above, the CMP engine extracts the primary key from the<br />

ResultSet based on the primary key's type. The property ignoreOnInsert must also be<br />

set to the name of the identity column. The CMP engine will then know not to set that<br />

column in the INSERT.<br />

164 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!