user manual
134 BES Developer’s Guide
Implementing an entity bean
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.
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.
The primary key class can be specific to an particular entity bean class. That
is, each entity bean can define its own primary key class. Or, multiple entity
beans can share the same primary key class.
The bank application uses two different entity beans to represent savings and
checking accounts. Both types of accounts use the same field to uniquely
identify a particular account record. In this case, they both use the same
primary key class, AccountPK, to represent the unique identifier for either type
of account. The following code shows the definition of the account primary key
class:
public class AccountPK implements java.io.Serializable {
public String name;
public AccountPK() {}
public AccountPK(String name) {
this.name = name;
}
}
Generating primary keys from a custom class
To generate primary keys from a custom class, you must write a class that
implements the com.borland.ejb.pm.PrimaryKeyGenerationListener interface.
Support for composite keys
Primary keys are not restricted to a single column. Sometimes, a primary key
is composed of more than one column. In a more realistic example, a course
is not identified merely by its name. Instead, the primary key for each course
record can be the department in which the course is offered and the course
number itself. The department code and the course number are separate
columns in the Course table. A select statement that retrieves a particular
course, or all courses in which a student is enrolled, must use the entire
primary key; that is, it must consider both columns of the primary key.
The Borland CMP engine supports composite primary keys. You can use keys
with multiple columns in the where clause of a select statement. You can also
select all fields of a compound key in the select clause portion of the
statement.










