Oracle TopLink with EJB CMP 2.0 Beans

How-To Document

January 2006

After reading this How-To document, you will be able to:

Software Requirements

Conventions

The following conventions are used in this document:

Introduction

This How-To document demonstrates how to use TopLink EJB 2.0 container-managed persistence (CMP) entity bean on an application server. An application with a Java client accesses multiple instances of a single TopLink-enabled persistent EJB type. The EJB is stored in a database which is accessed through the server connection facilities.

Single Bean

One possible configuration is to use a single TopLink EJB 2.0 CMP entity bean on the application server.

Configuration

The ejb-jar.xml file specifies CMP 2.0 entity beans.

Sample ejb-jar.xml File

<ejb-jar>
  <enterprise-beans>
    <entity>
      <ejb-name>Account</ejb-name>
      <local-home>examples.ejb.cmp20.singlebean.AccountHome</local-home>
      <local>examples.ejb.cmp20.singlebean.Account</local>
      <ejb-class>examples.ejb.cmp20.singlebean.AccountBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.math.BigDecimal</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
...

The deployment XML the TopLink Workbench generates specifies a single entity bean descriptor and a simple EJBQL read-all query.

Sample Deployment XML

<toplink:object-persistence ...>
   <opm:name>Account</opm:name>
   <opm:class-mapping-descriptors>
      <opm:class-mapping-descriptor xsi:type="toplink:relational-class-mapping-descriptor">
         <opm:class>examples.ejb.cmp20.singlebean.AccountBean</opm:class>
         <opm:alias>AccountBean</opm:alias>
         <opm:primary-key>...</opm:primary-key>
         <opm:querying>
            <opm:queries>
               <opm:query toplink:name="findLargeAccounts" xsi:type="toplink:read-all-query">
                  <arguments>...</arguments>
                  <toplink:bind-all-parameters>false</toplink:bind-all-parameters>
                  <toplink:cache-statement>false</toplink:cache-statement>
                  <toplink:call xsi:type="ejbql-call">
                     <toplink:ejbql>SELECT OBJECT(account) FROM AccountBean account WHERE account.balance > ?1</toplink:ejbql>
                  </toplink:call>
             <toplink:reference-class>examples.ejb.cmp20.singlebean.AccountBean</toplink:reference-class>
                  <toplink:cache-usage>conform</toplink:cache-usage>
               </opm:query>
            ... (other queries)
            </opm:queries>
         </opm:querying>
         <opm:attribute-mappings>...(mapped attributes)</opm:attribute-mapping>
         <toplink:descriptor-type>independent</toplink:descriptor-type>
         <toplink:sequencing>
            <toplink:sequence-name>ACCOUNT_SEQ</toplink:sequence-name>
            <toplink:sequence-field table="EJB_ACCOUNT" name="ACCOUNT_ID" xsi:type="opm:column"/>
         </toplink:sequencing>
         <toplink:caching>
            <toplink:always-conform>true</toplink:always-conform>
         </toplink:caching>
         <toplink:instantiation/>
         <toplink:copying/>
         <toplink:tables>
            <toplink:table name="EJB_ACCOUNT"/>
         </toplink:tables>
      </opm:class-mapping-descriptor>
   </opm:class-mapping-descriptors>
   <toplink:login xsi:type="database-login">
   ... (database login information)
   </toplink:login>
</toplink:object-persistence>

Creating

To create a new account for Peter Jones, TopLink executes the following in a unit of work:

ejbCreate(): AccountBean id: (null) owner: (Peter Jones) balance: (0.0)
UPDATE EJB_ACCOUNT_SEQ SET SEQ_VALUE = SEQ_VALUE + 50 WHERE SEQ_NAME = 'ACCOUNT_SEQ'
SELECT SEQ_VALUE FROM EJB_ACCOUNT_SEQ WHERE SEQ_NAME = 'ACCOUNT_SEQ'
INSERT INTO EJB_ACCOUNT (ACCOUNT_ID, BALANCE, OWNER) VALUES (861, 0.0, 'Peter Jones')

TopLink executes the unit of work based on the following Java code:

try {
  account = createAccount(null, 0.0, "Peter Jones");
  } catch (Exception exception) {
  ...
  }

public void createAccount(String anAccountNumber, double aBalance,  String anOwner) throws CreateException, NamingException, RemoteException {
  try {
    AccountHome home = getAccountHome();
    Account account = null;
    account = home.create(anAccountNumber, aBalance, anOwner);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
  }

TopLink Dynamic Finder Queries

Implementation for dynamic finders is automatically provided by TopLink; you simply need to add them to the home interface. For example, to find a single account:

public Account findOneBySQL(String queryString, java.util.Vector params) throws FinderException;

Relationships and Advanced Features

You can also use several TopLink advanced features:

Refer to the Oracle TopLink Examples for more information.

EIS System

The TopLink EIS (Enterprise Information System) can also be used with EJB 2.0 entity beans. For example, a single EJB 2.0 entity bean can be invoked from a series of Java Server Pages (JSP). The entity bean is mapped through TopLink to a non-relational EIS (via JCA).

Summary

This How-To has shown examples of using TopLink EJB 2.0 container-managed persistence (CMP) entity bean on an application server. A downloadable example of this How-To (including EIS implementation) for Oracle Containers for J2EE 10g Release 3 (10.1.3) is available from OTN.


Oracle TopLink, 10g Release 3 (10.1.3)

The Programs (which include both the software and documentation) contain proprietary information; they are provided under a license agreement containing restrictions on use and disclosure and are also protected by copyright, patent, and other intellectual and industrial property laws. Reverse engineering, disassembly, or decompilation of the Programs, except to the extent required to obtain interoperability with other independently created software or as specified by law, is prohibited.

The information contained in this document is subject to change without notice. If you find any problems in the documentation, please report them to us in writing. This document is not warranted to be error-free. Except as may be expressly permitted in your license agreement for these Programs, no part of these Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose.

If the Programs are delivered to the United States Government or anyone licensing or using the Programs on behalf of the United States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the Programs, including documentation and technical data, shall be subject to the licensing restrictions set forth in the applicable Oracle license agreement, and, to the extent applicable, the additional rights set forth in FAR 52.227-19, Commercial Computer Software—Restricted Rights (June 1987). Oracle Corporation, 500 Oracle Parkway, Redwood City, CA 94065

The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other inherently dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe, backup, redundancy and other measures to ensure the safe use of such applications if the Programs are used for such purposes, and we disclaim liability for any damages caused by such use of the Programs.

Oracle, JD Edwards and PeopleSoft are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

The Programs may provide links to Web sites and access to content, products, and services from third parties. Oracle is not responsible for the availability of, or any content provided on, third-party Web sites. You bear all risks associated with the use of such content. If you choose to purchase any products or services from a third party, the relationship is directly between you and the third party. Oracle is not responsible for: (a) the quality of third-party products or services; or (b) fulfilling any of the terms of the agreement with the third party, including delivery of products or services and warranty obligations related to purchased products or services. Oracle is not responsible for any loss or damage of any sort that you may incur from dealing with any third party.

Alpha and Beta Draft documentation are considered to be in prerelease status. This documentation is intended for demonstration and preliminary use only. We expect that you may encounter some errors, ranging from typographical errors to data inaccuracies. This documentation is subject to change without notice, and it may not be specific to the hardware on which you are using the software. Please be advised that prerelease documentation in not warranted in any manner, for any purpose, and we will not be responsible for any loss, costs, or damages incurred due to the use of this documentation.