Entity Beans Tutorial - PowerPoint PPT Presentation

Loading...

PPT – Entity Beans Tutorial PowerPoint presentation | free to download - id: ddcc-NmNkN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Entity Beans Tutorial

Description:

Entity Beans Tutorial. 5. EJB Object Identity. Each entity has a unique identifier ... Entity Beans Tutorial. 16. Create Example ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 34
Provided by: bill52
Category:
Tags: beans | entity | tutorial

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Entity Beans Tutorial


1
Entity Beans Tutorial
2
Entity Beans
  • Represent business entity objects
  • Persistent
  • Shared access
  • Transactional
  • Survive server crashes

3
Entity vs Session Beans
  • Entity Bean
  • business entity
  • shared access
  • long-lived
  • survive crashes
  • e.g. Account
  • Session Bean
  • task, agent
  • not shared
  • short-lived (relative)
  • lost on crashes
  • e.g. shoppingCart

4
Persistence
  • An entity bean can represent
  • object view of a record in the database
  • object view of an existing application
  • Persistence can be implemented by
  • bean itself (bean-managed persistence)
  • container (container-managed persistence)

5
EJB Object Identity
  • Each entity has a unique identifier
  • Represented by primary key class specific to each
    bean class
  • Container associates instances with object
    identity dynamically

6
Writing an Entity Bean
  • Enterprise Beans home interface
  • Enterprise Beans remote interface
  • Enterprise Bean class
  • Primary Key class

7
Entity Bean Example
  • An entity bean that represents a customer account
  • Persistence is managed by the bean using JDBC
    (bean-managed persistence)
  • Primary key class is String (ssn)

8
Remote Interface
  • Business method declarations
  • Exposes business methods to clients
  • Extends javax.ejb.EJBObject interface
  • All methods follow Java RMI rules

9
Remote Interface Example
  • public interface Account extends EJBObject
  • public void debit(double amount) throws
  • InsufficientBalanceException, RemoteException
  • public void credit(double amount) throws
  • RemoteException
  • public double getBalance() throws
  • RemoteException
  • public void changeName(String name) throws
  • RemoteException

10
Home Interface
  • Allows a client to create, locate or destroy a
    bean
  • Extends javax.ejb.EJBHome interface
  • All methods follow Java RMI rules
  • Create methods
  • Finder methods
  • Must provide findByPrimaryKey

11
Home Interface Example
  • public interface AccountHome extends EJBHome
  • public Account create(String ssn, String name)
  • throws CreateException, RemoteException
  • public Account findByPrimaryKey(String ssn)
  • throws FinderException, RemoteException
  • public Account findByName(String name)
  • throws FinderException, RemoteException

12
Enterprise Bean Class
  • Must implement javax.ejb.EntityBean interface
  • Must provide implementation of
  • business methods
  • ejbCreate, ejbPostCreate methods
  • ejbFindXXX methods

13
Enterprise Bean Example
  • public class AccountBean implements EntityBean
  • // entity state
  • private String name
  • private double balance
  • // resources
  • private java.sql.Connection con
  • private EntityContext context
  • // … various method implementations …

14
Business Methods
  • Matches signatures declared in Remote interface
  • Example
  • public void debit(double amount) throws
  • InsufficientBalanceException, RemoteException
  • if (balance - amount
  • throw new InsufficientBalanceException()
  • balance - amount

15
Create
  • ejbCreate
  • returns primary key type
  • matches signatures of create methods in Home
    interface
  • inserts a record representing the entity into the
    database
  • initializes instance variables

16
Create Example
  • public String ejbCreate(String ssn, String name)
    throws CreateException, RemoteException
  • try
  • // insert record into db
  • Statement stmt con.createStatement()
  • stmt.executeUpdate(INSERT INTO ACCOUNT …)
  • stmt.close()
  • catch (SQLException ex)
  • throw new CreateException()
  • // initialize state
  • this.name name
  • this.balance 0.0
  • return ssn // return primary key

17
PostCreate
  • ejbPostCreate
  • called after ejbCreate
  • EJB object identity is available (via
    EntityContext.getPrimaryKey)

18
Finder
  • ejbFindXXX
  • matches signature of finder methods in Home
    interface
  • returns primary key type or java.util.Enumeration
  • locates one or more entity objects in the
    database
  • Must define ejbFindByPrimaryKey method

19
Finder Example 1
  • public String ejbFindByPrimaryKey(String ssn)
  • throws FinderException, RemoteException
  • try
  • Statement stmt con.createStatement()
  • ResultSet rs stmt.executeQuery(SELECT SSN
    …)
  • if (rs.next()) // record exists?
  • stmt.close()
  • return ssn
  • else
  • stmt.close()
  • return null
  • catch (SQLException ex)
  • throw new FinderException()

20
Finder Example 2
  • public Enumeration ejbFindByName(String name)
  • throws FinderException, RemoteException
  • try
  • Vector v new Vector()
  • Statement stmt con.createStatement()
  • ResultSet rs stmt.executeQuery(SELECT SSN
    …)
  • while (rs.next())
  • v.addElement(rs.getString(1))
  • stmt.close()
  • return v.elements()
  • catch (SQLException ex)
  • throw new FinderException()

21
EntityBean Interface
  • setEntityContext,
  • unsetEntityContext
  • ejbActivate,
  • ejbPassivate
  • ejbRemove
  • ejbLoad,
  • ejbStore

22
Entity Context
  • setEntityContext
  • container passes reference to EntityContext
  • bean allocates resources held for the beans
    lifetime
  • unsetEntityContext
  • bean releases resources allocated in
    setEntityContext

23
Entity Context Example
  • public void setEntityContext(EntityContext
    context) throws RemoteException
  • this.context context
  • public void unsetEntityContext() throws
    RemoteException
  • // empty implementation

24
Activation
  • ejbActivate
  • container associates EJB object identity with an
    instance
  • bean allocates additional resources needed in
    ready state
  • ejbPassivate
  • container dissociates identity from instance
  • bean frees resources allocated in ejbActivate

25
Activation Example
  • public void ejbActivate() throws RemoteException
  • // set up JDBC connection
  • Properties prop context.getEnvironment()
  • String url prop.getProperty(url)
  • try
  • this.con DriverManager.getConnection(url)
  • catch (SQLException ex)
  • throw new RemoteException()

26
Passivation Example
  • public void ejbPassivate() throws RemoteException
  • // free JDBC connection
  • try
  • con.close()
  • catch (SQLException ex)
  • throw new RemoteException()

27
Load and Store
  • ejbLoad
  • synchronizes instance variables from entity state
    in database
  • refreshes states from database
  • ejbStore
  • synchronizes entity state in data store with
    instance variables
  • writes states to database

28
Load Example
  • public void ejbLoad() throws RemoteException
  • // get primary key
  • String ssn (String) context.getPrimaryKey()
  • // retrieve state from DB
  • try
  • Statement stmt con.createStatement()
  • ResultSet rs stmt.executeQuery
  • (SELECT … ACCOUNT WHERE SSN …)
  • if (rs.next())
  • name rs.getString(1)
  • balance rs.getDouble(2)
  • else
  • throw new RemoteException()
  • catch (SQLException ex)
  • throw new RemoteException()

29
Store Example
  • public void ejbStore() throws RemoteException
  • String ssn (String) context.getPrimaryKey()
  • try
  • Statement stmt con.createStatement()
  • stmt.executeUpdate(UPDATE ACCOUNT SET …)
  • catch (SQLException ex)
  • throw new RemoteException()

30
Remove
  • ejbRemove
  • removes entity representation in the database
  • releases resources allocated in ejbActivate

31
Remove Example
  • public void ejbRemove() throws RemoveException,
    RemoteException
  • String ssn (String) context.getPrimaryKey()
  • try
  • Statement stmt con.createStatement()
  • stmt.executeUpdate
  • (DELETE FROM ACCOUNT WHERE SSN …)
  • stmt.close()
  • con.close() // free JDBC connection
  • catch (SQLException ex)
  • throw new RemoteException()

32
Container-Managed Persistence
  • containerManagedFields deployment descriptor
  • All container-managed fields declared as public
  • Empty implementation for most ejbXXX methods
  • No need to implement ejbFindXXX

33
Summary
  • Entity bean represents a business entity
  • Enterprise Beans home interface
  • Enterprise Beans remote interface
  • Enterprise Bean class
  • Primary Key class
About PowerShow.com