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

Implementing an entity bean ■ ■ ■ Security Roles: simple definitions of security roles used by the application. Any security role references you defined for your beans must also be defined here. Method Permissions: each method of each bean can have certain rules about their execution. These are set here. Container-Transactions: this specifies the transaction attributes as per the EJB 2.0 specification for each method participating in a transaction ■ Exclude List: methods to be uncalled by anyone All of these can be accessed through the Deployment Descriptor Editor. You should refer to the EJB 2.0 specification for DTD information and the proper use of the descriptor files. Entity Bean Primary Keys Each Entity Bean must have a unique primary key that used to identify the bean instance. The primary key can be represented by a Java class that 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. 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; } } Chapter 14: Entity Beans and CMP 1.1 in Borland Enterprise Server 113

Implementing an entity bean 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. For the where clause, specify multiple field names in the same manner that you specify single field names. Use “and” to separate each field. The format is = :[ejb/] Note that the equal (=) sign is one of several possible notations. You could also specify greater than (>), less than (=), or less than or equal (

Implementing an entity bean<br />

■<br />

■<br />

■<br />

Security Roles: simple definitions of security roles used by the application. Any<br />

security role references you defined for your beans must also be defined here.<br />

Method Permissions: each method of each bean can have certain rules about<br />

their execution. These are set here.<br />

Container-Transactions: this specifies the transaction attributes as per the EJB<br />

2.0 specification for each method participating in a transaction<br />

■<br />

Exclude List: methods to be uncalled by anyone<br />

All of these can be accessed through the Deployment Descriptor Editor. You should<br />

refer to the EJB 2.0 specification for DTD information and the proper use of the<br />

descriptor files.<br />

Entity Bean Primary Keys<br />

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

instance. The primary key can be represented by a Java class that 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 />

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 />

The primary key class can be specific to an particular entity bean class. That is, each<br />

entity bean can define its own primary key class. Or, multiple entity beans can share<br />

the same primary key class.<br />

The bank application uses two different entity beans to represent savings and checking<br />

accounts. Both types of accounts use the same field to uniquely identify a particular<br />

account record. In this case, they both use the same primary key class, AccountPK, to<br />

represent the unique identifier for either type of account. The following code shows the<br />

definition of the account primary key class:<br />

public class AccountPK implements java.io.Serializable {<br />

public String name;<br />

public AccountPK() {}<br />

public AccountPK(String name) {<br />

this.name = name;<br />

}<br />

}<br />

Chapter 14: Entity Beans and CMP 1.1 in <strong>Borland</strong> Enterprise Server 113

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

Saved successfully!

Ooh no, something went wrong!