Creating the QuickBookingBean Entity Bean

You will now create a new entity bean named the QuickBookingBean. This bean will manage the data in the table QCR_RESERVATION. In particular, it will enable you to create new records and change or delete existing data.

Prerequisites

The structure of your project QuickCarRentalEjb is currently displayed in the J2EE Explorer.

Procedure

Specifying the general properties of the EJB

...

       1.      In the J2EE Explorer, to open the relevant wizard, choose New ® EJB… from the context menu of the project node QuickCarRentalEjb.

       2.      In the New EJB wizard that appears, assign the following values to the properties of the new EJB:

   

EJB Name

QuickBookingBean

EJB Project

QuickCarRentalEjb

Bean Type

Entity Bean

Default EJBPackage

com.sap.examples.quickcarrental

Generate default interfaces

activated

       3.      Choose Next.

The wizard displays the screen containing names of the remote and the local interfaces.

       4.      Accept the suggested values and choose Next.

In the wizard screen that appears, you will be able to specify the persistence properties.

       5.      Choose Container Managed Persistence as the Persistence type.

Specifying the primary key and other CMP fields

Every instance of an entity bean is identified by means of a unique key. For this purpose, you will first create a field named bookingId and define it as the primary key. Each instance of the entity bean QuickBookingBean will be uniquely identified with this key field. You will then add other CMP fields, which stored the associated records for the table QCR_RESERVATION.

You will map the table columns to the CMP fields of the entity bean as follows:

Column in QCR_RESERVATION

Entity bean field (CMP field)

ID

bookingId

VEHICLETYPEID

vehicleTypeId

RESERVATIONDATE

reservationDate

DATEFROM

dateFrom

DATETO

dateTo

PICKUPLOCATION

pickupLocation

DROPOFFLOCATION

dropoffLocation

STATUS

status

       6.      To add a field, in the left pane in the Wizard, select the Persistent Fields node and choose the Add button.

To the right input fields appear, where you can specify the field.

       7.      Enter the name bookingId and accept the suggested value java.lang.String for the type.

       8.      To define bookingId as the primary key field, in the left pane, choose the node PrimaryKey.

       9.      Choose Simple and assign bookingId as the primary key field.

   10.      To add the next field, in the left pane in the Wizard, select the Persistent Fields node again and choose the Add button.

   11.      Enter the name vehicleTypeId and accept the suggested value java.lang.String for the type.

   12.      To add the next field, in the left pane in the Wizard, select the Persistent Fields node and choose Add again.

   13.      Enter the name reservationDate and assign it the type java.util.Date.

If you choose the Full Qualified Name for the date using the Choose button, make sure that you select the correct package for this class when you search for the date class (by entering Date) – in this case, java.util.Date.

   14.      Add the other fields in a similar way, assigning them the following names and types:

Name

Type (Full Qualified Name)

dateFrom

java.util.Date

dateTo

java.util.Date

pickupLocation

java.lang.String

dropoffLocation

java.lang.String

status

java.lang.String

   15.      Choose Next.

In the wizard dialog that appears, you can define the methods of the entity bean.

Editing the ejbCreate method

You need to extend the standard method ejbCreate, so that the fields you have created – bookingId, vehicleTypeId, reservationDate, dateFrom, dateTo, pickupLocation, dropoffLocation and status – are passed to this method as parameters.

   16.      To add new parameters to the bean method ejbCreate, select the node of the same name under Create Methods in the tree structure.

Input fields appear to the right of the tree. You can change the method definition there.

   17.      Under Parameters, choose Add.

   18.      Enter the name bookingId and the type java.lang.String for the first parameter.

   19.      To add the other parameters, repeat the last two steps as appropriate. Use the same name and type assignment as specified above:

Name

Type (Full Qualified Name)

vehicleTypeId

java.lang.String

reservationDate

java.util.Date

dateFrom

java.util.Date

dateTo

java.util.Date

pickupLocation

java.lang.String

dropoffLocation

java.lang.String

status

java.lang.String

Always create parameters in the correct order, since this is the order used when ejbCreate is called.

Creating the finder method ejbFindByStatus

In the steps that follow you create an additional finder method named ejbFindByStatus. This method returns the bean instances of all the database entries that match a status value that has been passed as a parameter.

   20.      To create the new finder method ejbFindByStatus, select the Finder Methods node in the tree display (left) and choose the Add button.

Input fields appear to the right of the tree. You can define the new finder method there.

   21.      Enter the name ejbFindByStatus and assign it the return type java.util.Collection.

   22.      Under Parameters, choose Add, to add the parameter with the name status and the type java.lang.String.

   23.      Choose Finish to create the entity bean.

Result

The wizard generates the following: the bean class QuickBookingBean; the local interface QuickBookingLocal; remote interface QuickBooking; local home interface QuickBookingLocalHome and home interface QuickBookingHome, with the appropriate set and get, finder, and EJB default methods.

In the J2EE Explorer, a new substructure named QuickBookingBean is inserted automatically under QuickCarRental ® ejb-jar.xml.

Additionally a new deployment descriptor persistent.xml  has been created in the EJB Module project. You will need it later to assign the data source to the entity bean.

Next step:

Adding the Implementation of the ejbCreate Method