Flylib.com

Books Software

 
 
 

Writing the Entity EJB Class


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 name of the remote interface. The EJB class for Book is therefore BookBMPBean for the BMP example and BookCMPBean for the CMP example. The EJB class must be a public class that implements the javax.ejb.EntityBean interface.

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 methods when CMP is used.

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 Class

Entity 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 optionally provide a specific primary key class. The naming convention for the primary key class is to add the letters PK to the end of the remote interface name . The primary key for BookEntity is therefore BookEntityPK . The following rules must be followed when implementing a specific primary key class; it must

  • Implement java.io.Serializable .

  • Be constructed with the public default constructor (no parameters).

  • Contain a public attribute that has the same name and type as the primary key attribute in the entity bean.

  • Override the equals() method to compare based on the primary key attribute.

  • Override the hashCode() method to generate the hashcode based on the value of the primary key.

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 overrides the equals() and hashCode() methods . Listing 21.10 shows the implementation of the primary key class for the BookEntity EJB.

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();
    }
}