web server - Borland Technical Publications
web server - Borland Technical Publications web server - Borland Technical Publications
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
- Page 123 and 124: Container-managed persistence and R
- Page 125 and 126: Implementing an entity bean Generat
- Page 127 and 128: Container-Managed Persistence in Bo
- Page 129 and 130: Container-Managed Persistence in Bo
- Page 131 and 132: Setting Properties Setting Properti
- Page 133 and 134: Setting Properties into a BLOB. The
- Page 135 and 136: Setting Properties Automatic table
- Page 137 and 138: 126 BES Developer’s Guide
- Page 139 and 140: Container-managed persistence and R
- Page 141 and 142: Container-Managed Persistence in Bo
- Page 143 and 144: Container-Managed Persistence in Bo
- Page 145 and 146: Container-Managed Persistence in Bo
- Page 147 and 148: Container-Managed Persistence in Bo
- Page 149 and 150: Container-Managed Persistence in Bo
- Page 151 and 152: Container-Managed Persistence in Bo
- Page 153 and 154: 142 BES Developer’s Guide
- Page 155 and 156: Setting Properties J2EE 1.3 Entity
- Page 157 and 158: Setting Properties Figure 16.2 Edit
- Page 159 and 160: Setting Properties Table 16.1 ejb.m
- Page 161 and 162: Setting Properties Table 16.3 Table
- Page 163 and 164: Setting Properties Security Propert
- Page 165 and 166: Aggregate Functions in EJB-QL Selec
- Page 167 and 168: Support for ORDER BY Support for OR
- Page 169 and 170: Overriding SQL generated from EJB-Q
- Page 171 and 172: Container-managed data access suppo
- Page 173: 162 BES Developer’s Guide
- Page 177 and 178: Implementing primary key generation
- Page 179 and 180: Transaction manager services Consis
- Page 181 and 182: Transaction manager services When t
- Page 183 and 184: Transaction manager services Follow
- Page 185 and 186: Declarative transaction management
- Page 187 and 188: Declarative transaction management
- Page 189 and 190: JDBC API Modifications JDBC API Mod
- Page 191 and 192: Handling of EJB exceptions Applicat
- Page 193 and 194: 182 BES Developer’s Guide
- Page 195 and 196: Client View of an MDB Client View o
- Page 197 and 198: Clustering of MDBs This is yet anot
- Page 199 and 200: Error Recovery Redelivered messages
- Page 201 and 202: 190 BES Developer’s Guide
- Page 203 and 204: JNDI Definitions Module Important s
- Page 205 and 206: Disabling and Enabling a Deployed D
- Page 207 and 208: Configuring JDBC Datasources In the
- Page 209 and 210: Configuring JDBC Datasources To add
- Page 211 and 212: Defining the Connection Pool Proper
- Page 213 and 214: Defining the Connection Pool Proper
- Page 215 and 216: Descriptions of Borland Enterprise
- Page 217 and 218: Advanced Topics for Defining JDBC D
- Page 219 and 220: Connecting to JDBC Resources from J
- Page 221 and 222: Configuring JMS Connection Factorie
- Page 223 and 224: Defining Connection Pool Properties
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