Creating the Beans

The entity bean representing the CD is very easy to code, as it doesn't have to do a great deal. All the issues of persistence will be taken care of by the server. I will present the full code for this Bean below; the code for the CDCollection Bean will not be discussed further because it is not interesting in the context of container-managed persistence.

CD.java: remote interface for the "CD" Bean

package org.jboss.docs.cmp.cd.interfaces;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

/**
 * This interface defines the remote interface for the CD Bean
 */
public interface CD
extends EJBObject
{
   public Integer getId() throws RemoteException;

   public void setId(Integer id) throws RemoteException;

   public String getTitle() throws RemoteException;

   public void setTitle(String title) throws RemoteException;

   public String getArtist() throws RemoteException;

   public void setArtist(String artist) throws RemoteException;

   public String getType() throws RemoteException;

   public void setType(String type) throws RemoteException;

   public String getNotes() throws RemoteException;

   public void setNotes(String type) throws RemoteException;

}

The remote interface specifies methods to get and set the attributes of the object. That's all it needs to do in this example. Note that, as with any JavaBean, the names of these methods follow the standard convention; that is, if an attribute is called "String X" then the "get" and "set" methods should be defined as follows:

String getX();
void setX(String x);

However you can declare any methods that return information on the state of the entity bean. These methods must be implemented by the programmer in the implementation class of the entity bean.

CDHome.java: home interface for the "CD" Bean

package org.jboss.docs.cmp.cd.interfaces;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
import java.util.Collection;

/**
 * This interface defines the home interface for the CD Bean
 */
public interface CDHome
extends EJBHome
{

   /**
    * Create a new CD instance
    */
   public CD create(Integer id)
   throws RemoteException, CreateException;

   /**
    * Find the CD with the specified ID. This method is implemented by the
    * container
    */
   public CD findByPrimaryKey (Integer id)
   throws RemoteException, FinderException;

   /**
    * Finds the CD whose "type" attribute matches that specified.
    * This method is implemented by the container
    */
   public Collection findByType (String type)
   throws RemoteException, FinderException;

   /**
    * Get all CD instances. This method is implemented by the container
    */
   public Collection findAll()
   throws RemoteException, FinderException;

}

The important thing to note about this interface is that it specifies methods that don't need to be implemented. In this case, findByPrimaryKey(), findByType() and findAll() are all examples of "finder" methods. The EJB specification requires that the server be able to provide finder methods for all the persistent attributes in the object. So, for example, if your class has an attribute "X", then the server will provide a "findByX" method to search on that field. Note that with JBoss the search is "exact"; that is, it won't accept wild-card characters or an incorrect mixture of upper- and lower-case letters. The findByPrimaryKey() searches on the primary key field; we will discuss how the primary key is specified later.

CDBean.java: implementation of the "CD" Bean

package org.jboss.docs.cmp.cd.bean;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import java.rmi.RemoteException;

/**
 * This class contains the implementation for the methods specified in the home
 * and remote interfaces for the "CD" EJB
 */
public class CDBean
implements EntityBean
{
   EntityContext ctx;

   public Integer id;
   public String title;
   public String artist;
   public String type;
   public String notes;

   /**
    * Create an instance of a CD. Note that this method returns null because the real
    * creation is managed by the EJB container.
    */
   public Integer ejbCreate (Integer _id)
   {
      id = _id;
      return null;
   }

   /**
    * Called when the object has been instantiated; does nothing in this example
    */
   public void ejbPostCreate(Integer id) { }

   public String getTitle() { return title; }
   public void setTitle(String _title) { title = _title; }

   public Integer getId() { return id; }
   public void setId(Integer _id) { id = _id; }

   public String getArtist() { return artist; }
   public void setArtist(String _artist) { artist = _artist; }

   public String getType() { return type; }
   public void setType(String _type) { type = _type; }

   public String getNotes() { return notes; }
   public void setNotes(String _notes) { notes = _notes; }

   public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }

   public void unsetEntityContext() { ctx = null; }

   public void ejbActivate() { }
   public void ejbPassivate() { }
   public void ejbLoad() { }
   public void ejbStore() { }
   public void ejbRemove() { }

}

The CDBean class provides implementations of the methods that aren't provided automatically by the EJB container. Note that the ejbCreate method returns "null", meaning that the container should take care of initializing the instance in the server's process space. Because the CD Bean is essentially passive -- a data repository -- it only has a few methods.