• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Students need a concrete BMP (Bean-Managed Persistence) example that shows all pieces: remote interface, home interface, primary key class, bean class with JDBC code, deployment descriptor, and client code—without the fluff.

Core Idea

The Account Bean is a BMP entity bean representing a bank account. It demonstrates: Account remote interface, AccountHome home interface with finder methods, AccountPK primary key class, AccountBean with JDBC code for CRUD, ejb-jar.xml with <persistence-type>Bean</persistence-type>, and client code using JNDI lookups.

How It Works

1. Remote Interface (Account.java)

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
 
public interface Account extends EJBObject {
    public void deposit(double amt) throws RemoteException;
    public void withdraw(double amt) throws RemoteException, AccountException;
    public double getBalance() throws RemoteException;
}

2. Home Interface (AccountHome.java)

public interface AccountHome extends EJBHome {
    public Account create(String accountID, String ownerName) 
        throws RemoteException, CreateException;
    public Account findByPrimaryKey(AccountPK key) 
        throws RemoteException, FinderException;
    public double getTotalBankValue() 
        throws RemoteException, AccountException; // Home method
}

3. Primary Key Class (AccountPK.java)

public class AccountPK implements Serializable {
    public String accountID;
    public AccountPK() {}
    public AccountPK(String id) { this.accountID = id; }
}

4. Deployment Descriptor (ejb-jar.xml)

Key elements: <persistence-type>Bean</persistence-type>, <prim-key-class>examples.bmp.AccountPK</prim-key-class>, <resource-ref> for JDBC.

5. Client Code

Looks up Home via JNDI, calls home.create(), account.deposit(100), home.findByPrimaryKey(pk).

Visual Explanation

G Client Client AccountHome (Home Object) AccountHome (Home Object) Client->AccountHome (Home Object) JNDI lookup Account (EJB Object) Account (EJB Object) AccountHome (Home Object)->Account (EJB Object) create() AccountBean AccountBean Account (EJB Object)->AccountBean delegates Database Database AccountBean->Database JDBC (INSERT/UPDATE/SELECT) accounts table accounts table Database->accounts table

Key Properties

  • BMP: Bean writes all JDBC code (ejbCreate → INSERT, ejbLoad → SELECT, etc.)
  • AccountPK: Wrapper for primary key (String accountID)
  • Home methods: getTotalBankValue() runs from pool, not specific instance
  • Resource reference: <resource-ref> configures JDBC DataSource in JNDI
  • CMT: Deployment descriptor sets <transaction-type>Container</transaction-type>

Connections

Edge Cases & Gotchas

  • SQL exceptions: Must handle SQLException in BMP JDBC code
  • Withdraw validation: Check balance before withdrawing (throw AccountException)
  • Home methods: Run on pooled bean, not associated with specific EJB object
  • Resource reference: Must configure jdbc/bmp-account in container-specific descriptor
  • 12-mark question: Focus on interfaces, PK class, XML—not full JDBC code