Bean Managed Persistence (BMP) in
Entity Beans
Gopalan Suresh Raj
Note |
To
work with any of these samples, you will need the
following: .........................................JDK 1.1.6 or higher (I use JDK 1.1.7B) .........................................Swing 1.1 or higher for JDK 1.1 (required if you wanna use the HomeBase GUI) .........................................The HomeBase (Scunthorpe version 0.5.1) - a free EJB Server from http://ejbhome.iona.com/ |
Developing
a Bank Checking Account Server and Client
The Checking Account Server
component has methods for managing a typical Bank Checking
Account. Consequently, it has methods to create an account, and
credit & debit money from the Account.
The Steps involved in developing and deploying an Entity Bean are
1. Set up your Data Source to the Database
2. Define your Home Interface.
3. Define your Remote Interface.
4. Develop your EntityBean
5. Define a Primary Key class
*** Note : From this point onwards, whatever we do becomes vendor (HomeBase) specific ***
6. Define your Deployment Descriptor
7. Compile your classes and generate the Container code using the tools provided by the vendor
8. Startup your Server
1. Set up your Data Source to the Database
The example program requires a database table named CHECKINGS --the Data Manipulation Language (DML) of which is shown below-- with the following fields:
CREATE TABLE CHECKINGS ACCOUNT_NUMBER Number CUSTOMER_NAME Text BALANCE Currency *primary key (ACCOUNT_NUMBER)
2. Define your Home Interface
Bank\CheckingHome.java |
package
Bank; import
javax.ejb.*; public interface CheckingHome
extends EJBHome { |
3. Define your Remote Interface
Bank\Checking.java |
package
Bank; import
javax.ejb.*; public interface Checking
extends EJBObject { |
4. Develop your EntityBean
Bank\CheckingBean.java |
package
Bank; import
java.rmi.*; public class CheckingBean
implements EntityBean { ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try { ////////////////////////////////////////////////////////////////////
case
CheckingBean.BALANCE_FIELD: ////////////////////////////////////////////////////////////////////
case
CheckingBean.ALL_FIELDS: ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//STEP1 :
open the connection ////////////////////////////////////////////////////////////////////
//STEP1 :
open the connection //STEP1 :
open the connection ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
|
5. Define a Primary Key class
Bank\AccountKey.java |
package
Bank; public
interface AccountKey { |
Bank\CheckingPK.java |
package
Bank; import java.util.*; public class CheckingPK
implements AccountKey, java.io.Serializable
{ public int m_accountNumber; public void
setKey (int key) throws Exception { m_accountNumber
= key; |
*** Note : From this point onwards, whatever we do becomes vendor (HomeBase) specific ***
6. Write the Deployment Descriptor for your Server
I had to manually edit the ejbml file to replace container-managed with bean-managed (not that it really mattered one way or the other. This is probably a bug in HomeBase).
conf\Bank.ejbml |
<ejbml> <entity-bean name="checking" descriptor="Bank/CheckingDeployment" package="Bank" home="Bank.CheckingHome" remote="Bank.Checking" bean="Bank.CheckingBean" primary-key="Bank.CheckingPK" tx-attribute="TX_SUPPORTS" > <bean-managed storage-helper="com.ejbhome.generator.helpers.RelationalPersistenceCodeHelper" table="checking" data-source="" user="" password="" > </bean-managed> </entity-bean> </ejbml>
|
7. Compile your classes and generate the Container code using the tools provided by the vendor
MS-DOS Command Prompt |
H:\com\gopalan\AccountEJB>cd Bank H:\com\gopalan\AccountEJB\Bank>javac *.java H:\com\gopalan\AccountEJB\Bank>cd .. H:\com\gopalan\AccountEJB>java com.ejbhome.Deployer .\conf\Bank.ejbml EJBHome EJB Deployer version 0.5.1 (Scunthorpe) (c) Copyright IONA Technologies PLC 1999. All Rights Reserved. Windows NT x86 4.0 A nonfatal internal JIT (3.00.072b(x)) error 'regvarHI' has occurred in : 'com/ejbhome/Deployer.<init> (Ljava/util/Properties;Ljava/util/Vector;)V': Interpreting method. Please report this error in detail to http://java.sun.com/cgi-bin/bugreport.cgi Deploying: checking... Generating: IonaCheckingHome...done. Generating: IonaRemoteChecking...done. Generating: IonaCheckingBean...done. Generating: IonaCheckingContext...done. Implementing Communications: Home Stubs & Skels...done. Implementing Communications: Remote Stubs & Skels...done. Compiling: IonaCheckingHome.java...done. Compiling: IonaRemoteChecking.java...done. Compiling: IonaCheckingBean.java...done. Compiling: IonaCheckingContext.java...done. Compiling: IonaCheckingHome_Stub.java...done. Compiling: IonaCheckingHome_Skel.java...done. Compiling: IonaRemoteChecking_Stub.java...done. Compiling: IonaRemoteChecking_Skel.java...done. H:\com\gopalan\AccountEJB> |
8. Startup your Server
MS-DOS Command Prompt |
H:\com\gopalan\AccountEJB> H:\com\gopalan\AccountEJB>java com.ejbhome.Server com.ejbhome.server.EnterpriseServer, initializing. com.ejbhome.server.EnterpriseServer.fireContainerAdded() at (Compiled Code) com.ejbhome.VendorConfiguration.<init>() at (VendorConfiguration.java:57) com.ejbhome.VendorConfiguration.getSystemCodeHelper() at (VendorConfiguration.java:174) com.ejbhome.VendorConfiguration.getTransactionCodeHelper() at (VendorConfiguration.java:138) com.ejbhome.VendorConfiguration.getCommsCodeHelper() at (VendorConfiguration.java:163) com.ejbhome.VendorConfiguration.getTracingCodeHelper() at (VendorConfiguration.java:152) com.ejbhome.VendorConfiguration.getPersistenceCodeHelper() at (VendorConfiguration.java:125) com.ejbhome.VendorConfiguration.getSessionPassivationCodeHelper() at (VendorConfiguration.java:185) com.ejbhome.VendorConfiguration.getVendorPrefix() at (VendorConfiguration.java:100) starting RMI registry on port 1099... RMI registry on port 1099, has started. com.ejbhome.server.EnterpriseServer.bindDataSources() at (Compiled Code) com.ejbhome.naming.spi.rmi.RMICtx.<init>() at (RMICtx.java:20) registering XA data sources com.ejbhome.sql.XADataSourceImpl.<init>(jdbc:odbc:Bank,{password=mouse, user=user}) at (XADataSourceImpl.java:75) com.ejbhome.naming.spi.rmi.RMICtx.bind() at (RMICtx.java:168) com.ejbhome.server.EnterpriseServer.fireDataSourceAdded() at (Compiled Code) registered: Bank -> jdbc:odbc:Bank, as an XA pooled data source [main] com.ejbhome.naming.spi.rmi.RMICtx.<init>() at (RMICtx.java:20) loading beans from: Bank.ejbml com.ejbhome.server.EnterpriseServer$2.startElement(ejbml) at (EnterpriseServer.java:228) com.ejbhome.server.EnterpriseServer$2.startElement(entity-bean) at (EnterpriseServer.java:228) com.ejbhome.VendorConfiguration.getHomeContainerClassName() at (VendorConfiguration.java:226) com.ejbhome.VendorConfiguration.getVendorPrefix() at (VendorConfiguration.java:100) com.ejbhome.container.AbstractEJBHome.<init>() at (AbstractEJBHome.java:136) com.ejbhome.naming.spi.rmi.RMICtx.bind() at (RMICtx.java:168) registered Bank.IonaCheckingHome, as: checking. com.ejbhome.server.EnterpriseServer$2.startElement(bean-managed) at (EnterpriseServer.java:228) finished loading beans from: Bank.ejbml |
The Server is now up and ready and is waiting for connections from the client.
Develop the Client Code
CheckingClient.java |
import java.rmi.*; import java.util.*; import Bank.*; public class CheckingClient {
////////////////////////////////////////////////////////////////////
server = home.findByPrimaryKey (key);
name = server.getCustomerName (); public
static void main(String[] args) { |
Compile and Run the Client
MS-DOS Command Prompt |
H:\com\gopalan\AccountEJB>javac CheckingClient.java H:\com\gopalan\AccountEJB>java CheckingClient Naming.lookup successful... Invoking CheckingsPK::setKey Account = 11 Customer = Athul Raj Balance = 100032.32 Invoking CheckingsPK::setKey Account = 12 Customer = Adithya Ramesh Balance = 50000.15 Invoking CheckingsPK::setKey Account = 13 Customer = Somebody Balance = 132.12 Invoking CheckingsPK::setKey Account = 14 Customer = Somebody Balance = 45.32 H:\com\gopalan\AccountEJB> |
Download the entire source code as a zip file.
click here to go to
My EJB
HomePage...
click here to go
to
My
Advanced Java Tutorial Page...
About the Author... |
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com. |
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited times since September 21,1998. |
Last Updated : Mar 27, '99 |
Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use. |
All products and companies mentioned at this site are trademarks of their respective owners. |