Writing the Entity EJB Class
The EJB class provides the API for mapping the attributes of the entity bean to the persistent storage. The naming convention for the EJB class used in this chapter is to append
BMPBean
or
CMPBean
to the
The code written by the bean provider for the implementation of the entity bean is significantly different depending on whether BMP or CMP is being used to manage persistence. The first notable difference is that a CMP entity EJB is written as an abstract class, whereas a BMP entity EJB is fully implemented by the bean provider as a concrete class. This is true because the getters and setters for the container-managed fields are abstract
Refer to the "Bean-Managed Persistence Example" for further information on implementing BMP, and the "Container-Managed Persistence Example" for further information on implementing CMP. |
Writing the Primary Key ClassEntity EJBs must include a primary key that is used to uniquely identify an instance of the entity bean. The entity bean returns an instance of the primary key class from the ejbCreate() method. The primary key class may be a standard Java class, such as java.lang.String or java.lang.Integer .
The bean provider may
Tip If the EJB deployment descriptor uses <automatic-key-generation> , the primary key type must be java.lang.Integer . Do not write a specific primary key class for the entity bean if you intend to use automatic key generation. This option is explained in the section, "The weblogic-cmp-rdbms-jar.xml Persistence Descriptor" later in this chapter. Creating the BookEntityPK Primary Key Class
The primary key for
BookEntity
is the
bookId
field, which is a
String
. The implementation for the specific primary key class for
BookEntity
contains the
bookId
as a public attribute and
Listing 21.10 Sample Primary Key Class for the BookEntity EJB
/**
* Primary Key for the BookEntity EJB
*/
package com.objectmind.BookStore;
import java.io.Serializable;
public class BookEntityPK implements Serializable
{
// primary key attribute
public String bookID;
/**
* override equals() to compare against bookId
*/
public boolean equals( Object that )
{
if((that == null ) !(that instanceof BookEntityPK))
return false;
return bookID.equals( ((BookEntityPK)that).bookID );
}
/**
* override hashCode()
*/
public int hashCode()
{
return bookID.hashCode();
}
}
|