Oracle9iAS TopLink CMP for Users of BEA WebLogic Guide Release 2 (9.0.3) Part Number B10065-01 |
|
This chapter introduces the basic concepts that are required to build and deploy an entity bean with TopLink. It provides an example of how TopLink CMP is used in a simple application that combines Java server pages (JSPs) and EJBs. A simple entity bean is used to illustrate TopLink's basic direct-to-field mapping capabilities, along with some simple EJBQL queries.
Included in this example are instructions for
The example package is examples.ejb.cmp20.singlebean
.
See <INSTALL_DIR>
/doc/demos.html
for links to all the examples and details on configuring the examples for WebLogic Application Server.
This chapter guides you through running the example, as well as describing in detail how the example was built. Although the Single Bean example is a relatively simple application, the process and procedures it introduces can be used to build much more complex applications as well.
To run the Single Bean example, you must
TopLink includes HSQL, as well as a preconfigured database containing all of the tables required to run the example. The SQL commands required to recreate the database are found in the createTables.sql
file. Use the provided ResetDatabase.cmd
script to reset the database tables if required.
The Single Bean example shows how a single bean can be made persistent using TopLink Container-Managed Persistence CMP support. This example illustrates simple direct-to-field mappings and introduces the basic steps required to deploy a bean.
This is a simple example that demonstrates how to use entity beans when deployed using TopLink CMP. The example consists of an entity bean called Account
.
The Single Bean example demonstrates:
ejbFind
methods is not required in the entity bean - query logic for each finder method defined in the home interface is defined using the Mapping Workbench or in an amendment method
The client application is an HTML page generated by JSP components. The Single Bean example follows the basic J2EE application architecture, using pure HTML generated by JSPs. The JSPs access the Single Bean entity bean.
The example can be viewed by pointing a web browser at http://localhost:7001/Account/mainPage.jsp. Starting at this main page, you can query for single beans based on the associated balance and owner, and add and delete single beans.
The use of JSPs and EJBs in this example provides a simple example of a J2EE application, and demonstrates one of several different ways to leverage this technology.
The following table lists the packages, classes, and files associated with the Single Bean example in this chapter. All files associated with the example are located in <install>\examples\wlsxx\examples\ejb\cmp20\
unless otherwise noted.
singlebean
The Single Bean example provides a simplified view of the standard "bank account" example, and shows how a single class can be modeled as an entity bean and made persistent using TopLink.
The interface examples.ejb.cmp20.singlebean.Account
provides the public "local" interface for the bean. It extends the javax.ejb.EJBLocalObject
interface, and contains all of the business methods that are accessible to local clients of the entity. This includes getters and setters for the instance data, as well as deposit()
and withdraw()
methods.
The class examples.ejb.cmp20.singlebean.AccountBean
provides the actual bean implementation for the bank single bean. It has methods corresponding to the methods on the remote interface, as well as the methods required by the javax.ejb.EntityBean
interface, which it extends. The account
's fields include AccountId (String)
, balance (double)
, and owner (String)
.
The interface examples.ejb.cmp20.singlebean.AccountHome
provides the "local home" interface of the bean. It extends the javax.ejb.EJBLocalHome
interface, and defines the required create, remove, and finder methods.
In this example, no additional primary key class is used. In EJB 1.1 and above, additional primary key classes are not required if the primary key consists of one field.
The Single Bean data is stored in a single table.
Column Name | Column Type | Details |
---|---|---|
ACCOUNT_ID |
Numeric |
primary key |
BALANCE |
DOUBLE |
balance in single bean |
OWNER |
VARCHAR |
owner's name |
The example also makes use of table-based sequencing through the EJB_ACCOUNT_SEQ table. This table provides primary keys (account numbers) for the EJB_ACCOUNT table.
Name | Type | Details |
---|---|---|
SEQ_NAME |
VARCHAR |
Used by TopLink to identify the table that requires sequence data |
SEQ_VALUE |
DECIMAL |
Sequence data |
A deployable component is typically developed as follows:
Each entity can contain any or all of the following interfaces:
These interfaces dictate how the bean is used by other components of the application. The Account interfaces have been created and are located in the main "single bean" example directory.
Define abstract get
and set
methods to represent the persistent attributes and use the get
and set
methods when implementing the business logic in the beans. The AccountBean class has been created and is located in the main Single Bean Example directory.
Three deployment descriptors are required for each JAR. They are
The ejb-jar.xml
descriptor should be created as specified in the EJB 2.0 specification. The weblogic-ejb-jar.xml
file should be defined as described in WebLogic Server documentation. The toplink-ejb-jar.xml
file should be defined as described in this documentation. These descriptors have been created and are located in the example directory.
The deployment descriptors must be in the META-INF directory in the JAR.
This is the main deployment descriptor prescribed and specified by the EJB specification. It defines most of the basic properties for the session and entity beans, including
The key elements that are particularly relevant to TopLink CMP are:
A unique name (across all beans in the JAR) which must match the ejb-name
attribute in the weblogic-ejb-jar.xml
file.
Must be set to 2.x to indicate that EJB 2.0 is being used.
Lists the data source used by the entity in which it is declared.
A group of elements that, when combined, describe a particular bean finder by including the method name, parameter types and query criteria.
The use of all XML elements in the file are described in the EJB 2.0 specification.
This descriptor file is specific to WebLogic Server and offers an opportunity to override some of the configuration settings in the server. Some of the elements that may require modification are:
A section that sets the persistence type. Note that this tag is not required by WebLogic 7.0, as its function is included in the <persistence-use>
tag.
A section that specifies TopLink as the persistence storage mechanism.
For information on configuring the weblogic-ejb-jar.xml
file, see "Configuring entity bean deployment descriptors".
The TopLink deployment descriptor is included in the JAR in the same META-INF directory as the other two deployment descriptors. This descriptor provides the information that TopLink needs to deploy the entities in the JAR. Because the entities deployed in a JAR are all encompassed by a TopLink project, the deployment JAR file is associated with exactly one project. This project is in turn associated with exactly one TopLink session (as implied by the single session element in the descriptor).
The elements that have been modified for the Single Bean example in the toplink-ejb-jar.xml
file are:
A session name (unique among all deployed JARs) that is used as a key for the deployed TopLink project (or the JAR that contains the project).
The fully-qualified name of the TopLink project class. This class should be included in the deployable JAR file. The project class can either be generated by the Mapping Workbench or written manually.
Note:
You can use a |
A sub-element of login, the fully-specified connection pool specifies the connection pool URL of the data source.
For more information on the options available for the <connection-pool>
element, refer to the DTD in <install>\wls_cmp\toplink-wls-ejb-jar_903.dtd
.
This section describes the steps required to create the Single Bean project using the Mapping Workbench. It also introduces and addresses some issues related to mapping EJB 2.0 beans, such as relationships and reserved finders, which are not encountered in the Single Bean example.
For more information about creating projects using the Mapping Workbench, consult the Oracle91AS TopLink Mapping Workbench Reference Guide.
This section assumes you have already read and completed the introductory tutorials in Oracle9iAS TopLink Tutorials, which offers an introduction to the fundamental concepts of the Mapping Workbench.
A TopLink project defines how the entity beans are to be persisted to the database. The Mapping Workbench can be used to easily build a TopLink project. The project is specified in the toplink-ejb-jar.xml
in the <project-class>
or <project-xml>
element and used at runtime to persist the beans.
ejb-jar.xml
file to use for the project. You can either choose an existing file or create a new one. The ejb-jar.xml
file is typically created by another tool, and the mapping information it contains is read and used by the Mapping Workbench (see step 7 below).
If no ejb-jar.xml
file is available, the Mapping Workbench can be used to create one and to populate the file with the information the Mapping Workbench requires. Once the file is created, however, other elements usually need to be added to the ejb-jar.xml
file before it can be used with ejbc and in a deployable JAR.
When the Mapping Workbench project is saved, changes to the ejb-jar.xml file
can also be written out. See "Working with the ejb-jar.xml file" in the Oracle9iAS TopLink Mapping Workbench Reference Guide for details on working with the ejb-jar.xml file.
cmp
and cmr
fields do not appear on the AccountBean descriptor.
ejb-jar.xml
file for cmp
and cmr
fields.
After setting the AccountBean to be an EJB descriptor, the cmp
and cmr
fields appear and are available for mapping.
ejb-jar.xml
file by selecting the project and clicking Selected > Update Project from ejb-jar.xml. This is an optional step that is not required if you are generating the ejb-jar.xml
file in the Mapping Workbench. However, the the Single Bean example uses an existing ejb-jar.xml
file, so using it to update the project brings in two user-defined finders which appear in the AccountBean descriptor's Queries tab.
The Mapping Workbench reads from and writes to the following elements in the ejb-jar.xml
:
cmp
and cmr
fields. The AccountBean has three cmp
fields to be mapped using direct-to-field mappings: accountId, balance, and owner. Map them to their corresponding database fields in the EJB_ACCOUNT table.
toplink-ejb-jar.xml
file either the <project-class>
or <project-xml>
element is used depending on which export method was used.
The Single Bean example is packaged into and EAR file, which itself contains the following:
A build script is included in the ...\singlebean
example directory which compiles the bean classes, runs weblogic.ejbc, and creates the EAR file. Running the build script places a copy of the EAR file in both the Single Bean directory and the server's deployment directory.
Windows users can use the build script as provided. Users of other platforms can use the build script as a template to create their own build script.
The build script can be modified and used to build deployable EAR or JAR files for other applications.
The build.cmd file uses the environment information in the <install>/setenv.cmd file
(Under Windows) or the setenv.sh
file (non-Windows). To avoid possible errors, ensure that your environment also matches these settings before running the script.
This can be done a number of different ways. See the WebLogic Server documentation for more detailed information on how to deploy an EJB JAR or an EAR into the server.
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|