12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter<br />

18<br />

18Generating Entity Bean<br />

Chapter<br />

Primary Keys<br />

Each entity bean must have a unique primary key that is used to identify the bean<br />

instance. The primary key can be represented by a Java class, which must be a legal<br />

value type in RMI-IIOP. Therefore, it extends the java.io.Serializable interface. It<br />

must also provide an implementation of the Object.equals(Object other) and<br />

Object.hashCode() methods.<br />

Normally, the primary key fields of entity beans must be set in the ejbCreate() method.<br />

The fields are then used to insert a new record into the database. This can be a difficult<br />

procedure, however, bloating the method, and many databases now have built-in<br />

mechanisms for providing appropriate primary key values. A more elegant means of<br />

generating primary keys is for the user to implement a separate class that generates<br />

primary keys. This class can also implement database-specific programming logic for<br />

generating primary keys.<br />

You may either generate primary keys by hand, use a custom class, or allow the<br />

container to use the database tools to perform this for you. If you use a custom class,<br />

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

in “Generating primary keys from a custom class” on page 164. To use the database<br />

tools, you can set properties for the CMP engine to generate primary keys depending<br />

upon the database vendor. See “Implementing primary key generation by the CMP<br />

engine” on page 164.<br />

Generating primary keys from a user class<br />

With enterprise beans, the primary key is represented by a Java class containing the<br />

unique data. This primary key class can be any class as long as that class is a legal<br />

value type in RMI-IIOP, meaning it extends the java.io.Serializable interface. It must<br />

also provide an implementation of the Object.equals(Object other) and<br />

Object.hashCode() methods, two methods which all Java classes inherit by definition.<br />

Chapter 18: Generating Entity Bean Primary Keys 163

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

Saved successfully!

Ooh no, something went wrong!