Prev | Next | J2EETM Developer's Guide
Entity Beans |
doc/guides/ejb/examples/product
directory:
)
)
)
)ProductEJB
class has the following container-managed fields:
These fields represent the state of anpublic String productId; public String description; public double price;
ProductEJB
instance. You specify the container-managed fields with the Application Deployment Tool (either in the New Enterprise Bean Wizard or on the Entity tab). A container-managed field must be one of the following types:
public
and may not be defined as transient
.
Using the Application Deployment tool, you define one (or more) of the container-managed fields as the primary key field (or fields) of the entity bean. For more information, see the section, Primary Key Class on page 66.
ejbCreate
method initializes the container-managed fields from the input parameters. The method returns null because with container-managed persistence the container ignores its return value. After the ejbCreate
method executes, the container inserts the container-manged fields into the database. Here is the ejbCreate
method of the ProductEJB
class:
public String ejbCreate(String productId, String description, double price) throws CreateException { if (productId == null) { throw new CreateException("The productId is required."); } this.productId = productId; this.description = description; this.price = price; return null; }
remove
method, the container calls the ejbRemove
method. After the ejbRemove
method returns, the container deletes the row from the database. If the container fails to delete the row, it throws an exception.
If an entity bean needs to perform some operation immediately before removal, it should do so in the ejbRemove
method. Because the ProductEJB
class does not have to perform such an operation, its ejbRemove
method is empty.
ejbLoad
methodejbLoad
method is empty. The entity bean may use the ejbLoad
method, however, to transform the values read from the database. For example, the ejbLoad
method might uncompress text data so that it can be manipulated by the business methods.
ejbStore
methodejbLoad
method, the ejbStore
method is typically empty. But if you need to transform container-managed fields before the container stores them in the database, you should do so in the ejbStore
method. For example, the ejbStore
method might compress text data before the container stores it in the database.
ProductHome
interface defines the following finder methods:
Because thepublic Product findByPrimaryKey(String productId) throws FinderException, RemoteException; public Collection findByDescription(String description) throws FinderException, RemoteException; public Collection findInRange(double low, double high) throws FinderException, RemoteException;
ProductEJB
class uses container-managed persistence, it does not implement these finder methods. The Application Deployment tool implements the findByPrimaryKey
method, including the SQL
select statement that retrieves the row from the database. The tool also implements the customized finder methods (findByDescription
and findInRange
), but you must specify the where
clauses for their select
statements. For instructions on specifying the where
clause, see the section, Specifying the Deployment Settings on page 63.
create
table
statement for an entity bean with container-managed persistence. For example, the tool generates the following statement for the ProductEJB
class:
Note: The double quotes are part of the table, column, and constraint names. In the preceding SQL statement, the table name isCREATE TABLE "ProductEJBTable" ( "description" VARCHAR(255) , "price" DOUBLE PRECISION NOT NULL , "productId" VARCHAR(255), CONSTRAINT "pk_ProductEJBTable" PRIMARY KEY ("productId") )
"ProductEJBTable"
-- not ProductEJBTable
.
By default, the EJB container executes the create
table
statement when you deploy the application containing the entity bean. (Also by default, it drops the table when you uninstall the application.) If you do not want the container to create the table during deployment, follow these steps:
1. In the Application Deployment Tool, select the Entity tab of the entity bean.
2. In the Entity tabbed pane, click Deployment Settings.
3. In the Deployment Settings dialog box, de-select the checkbox for "Create Table on Deploy."
2. Do not create the database table. The EJB container will create the table automatically.cloudscape -start
ProductEJB
.
a. Select the Entity radio button.
b. In the Display Name field, enter ProductBean
.
Entity Settings Dialog Box:
a. Select the radio button labelled "Container-Managed Persistence."
b. Select the check boxes for these container-managed fields:productId
,description
, andprice
.
c. In the Primary Key Class field, enter java.lang.String
.
d. In the Primary Key Field Name field, select productId.Transaction Management Dialog Box:
For the business methods, in the Transaction Type column select Required. (The business methods aregetDescription
,getPrice
, andsetPrice
.)
2. In the Entity tabbed pane, click Deployment Settings.
3. In the Database JNDI Name field, enter jdbc/Cloudscape
.
4. Verify that the checkbox is selected for "Create Table on Deploy."
6. A window pops open to inform you that the SQL statements have been generated for the Cloudscape database. Click OK.
7. A window pops open to inform you that you need to provide the SQL where
clause for the findByDescription
and findInRange
methods. Click OK.
8. In the EJB method list, select findByDescription
. A partial select
statement appears.
9. Add the following where
clause to the select
statement:
WHERE "description" = ?1
Note: You must include the quotes in the10. In the EJB method list, select"description"
column name. The?1
represents the first parameter of the finder method.
findInRange
. A partial select statement appears.
11. Add the following where
clause to the select
statement:
WHERE "price" BETWEEN ?1 AND ?2
Note: You must include the quotes in the12. Click OK."price"
column name. The?1
and?2
represent the first and second parameters of the finder method.
2. In the second dialog box, for the ProductBean enter MyProduct
in the JNDI Name field.