5577l1

Listing 1. BookBean.java, the bean class for our entity bean. This is
the code that actually does the work. In most simple cases (as with
this example), the bean class does little more than implement methods
to retrieve and modify instance variables.

package il.co.lerner.book;

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

import il.co.lerner.book.BookPK;

/**
   BookBean, the bean class for our entity bean. Each instance of
   BookBean represents a different book, which is represented in the
   back-end database by a single row in a database table.
*/

public class BookBean implements EntityBean
{
    EntityContext ctx;

    public int id;
    public String title;
    public String author;
    public String publisher;
    public double usDollarPrice;

    /* Each time create() is invoked on the home interface,
       ejbCreate() is invoked on our object with the same
       arguments. */
    public BookPK ejbCreate(int newId, String newTitle,
			    String newAuthor, String newPublisher,
			    double newUsDollarPrice)
    {
	// Assign our instance variables based on the arguments
	id = newId;
	title = newTitle;
	author = newAuthor;
	publisher = newPublisher;
	usDollarPrice = newUsDollarPrice;

	// Let the system handle the rest!
	return null;
    }

    /* After ejbCreate() is invoked, the system executes
       ejbPostCreate(), once again with the same arguments as create()
       and ejbCreate(). Our bean is simple enough that it doesn't
       need to do anything in ejbPostCreate(). */
    public void ejbPostCreate(int newId, String title,
			      String author, String publisher,
			      double usDollarPrice) { }

    /* We need to write one "setter" and one "getter" for each public
       instance variable. */

    public int getId() { return id; }
    public void setId(int newId) { id = newId; }

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

    public String getAuthor() { return author; }
    public void setAuthor(String newAuthor) { author = newAuthor; }

    public String getPublisher() { return publisher; }
    public void setPublisher(String newPublisher)
    {
	publisher = newPublisher;
    }

    public double getUsDollarPrice() { return usDollarPrice; }
    public void setUsDollarPrice(double newUsDollarPrice)
    {
	usDollarPrice = newUsDollarPrice;
    }

    /* We must implement the following methods */
    public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }
    public void unsetEntityContext() { ctx = null; }

    /* We must implement the following methods, but will leave them
       without procedure bodies. */
    public void ejbActivate() { }
    public void ejbPassivate() { }
    public void ejbLoad() { }
    public void ejbStore() { }
    public void ejbRemove() { }
}
