Entity Beans - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Entity Beans

Description:

home org.acme.employee.EmployeeHome /home remote org.acme.employee.Employee /remote ejb-class org.acme.employee.EmployeeBean /ejb-class ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 27
Provided by: conest
Category:
Tags: acme | beans | entity

less

Transcript and Presenter's Notes

Title: Entity Beans


1
Entity Beans
  • By
  • James Kinkel
  • Chris Bagley

2
Introduction
  • JavaBeans
  • A Java Bean is a software component that is
    reusable
  • Serializable
  • Used to temporarily store data
  • Entity Bean
  • Similar to a JavaBean
  • Has additional components to allow for
    persistence of data

3
Purpose
  • To easily save data from business classes to the
    database
  • To allows for the easy manipulation of data and
    reduce coding time

4
Entity Bean Defined
  • An Entity Bean is a class that contains a
    mechanism for persistence
  • Each Entity Bean should have an associated table
    in a database
  • They allow shared access, contain primary keys
    and can have relationships with other Entity
    Beans (ex. One-to-one, one-to-many, many-to-many)
  • Each instance of an entity bean represents a
    record in the database

5
Entity Bean Defined
  • Entity beans allow for shared access
  • Each bean, much like a record in a table, has a
    unique identifier
  • The beans can have
  • relationships between
  • them

6
Persistence
  • Persistence within an Entity Bean allows for the
    data contained within the beans properties to be
    stored
  • This can be done by using bean-managed or
    container-managed persistence

7
Persistence
  • Bean-managed persistence
  • Refers to calling methods that the developer will
    code within the bean
  • These methods contain calls to the database

8
Persistence
  • Container-managed persistence
  • Contains no SQL calls to the database
  • Not tied to a specific database
  • The abstract schema is used to define persistent
    fields and relationships
  • This schema is then used to generate virtual
    fields that are used for persistence

9
Advantages
  • Only one of each entity bean is created (based on
    records)
  • This means that all users are looking at the most
    current data for each record
  • Makes CRUD simple for developers
  • Uses an object then method call instead of a SQL
    call to the database
  • Simple to make changes
  • Change the schema then change the appropriate
    associated xml file

10
Disadvantages
  • Poor performance
  • When a get or set method is called, the beans
    load method must then be called, then the call to
    the database will be made
  • Direct call would connect to database and
    retrieve data
  • Entity beans are instantiated for each record
  • If you are returning 100 records, 100 instances
    of the bean will be created

11
Class Involvement
  • Who wants to volunteer???
  • Andrew?
  • George?
  • Illian?

12
Competing Technologies
  • To get the same results as an entity bean in the
    Microsoft world, this would be handled by
    designing a 3 or 4 tiered system.
  • The system would utilize a Data Access Layer
    (DAL) and a Business Logic Layer (BLL).
  • The data is encapsulated in a Business object
    and by utilizing business processing logic each
    particular business entity is processed.

13
Competing Technologies
  • The BLL isnt a persistent storage mechanism and
    business objects cannot store data indefinitely.
  • The BLL uses the DAL for long term data storage
    and retrieval.

14
Supporting Technologies
  • Entity beans are used within the JAVA environment
  • Entity beans can be used with Oracle databases as
    well as MS SQL
  • IBM WebSphere IDE supports entity bean development

15
Vendors
  • Sun Microsystems
  • IBM
  • NetBeans
  • Eclipse
  • JBoss

16
Code Entity Bean Class
  • import javax.ejb.
  • import java.rmi.RemoteException
  • public class EmployeeBean implements EntityBean
  • public int id
  • public String firstname
  • public String lastname
  • public String email
  • public EntityContext context
  • public EmployeeBean()
  • public Integer ejbCreate( String firstname,
    String lastname, String email ) throws
    CreateException
  • this.firstname firstname
  • this.lastname lastname
  • this.email email
  • return null
  • public void ejbPostCreate( String firstname,
    String lastname, String email ) throws
    CreateException

17
Code Entity Bean Class Cont.
  • public void says( String message )
  • System.out.println( "" firstname " "
    lastname "(" id ") _ email " "
    message )
  • public void setEntityContext( EntityContext
    context ) throws EJBException, _ RemoteException
  • this.context context
  • public void unsetEntityContext() throws
    EJBException, RemoteException
  • public void ejbActivate() throws EJBException,
    RemoteException
  • public void ejbLoad() throws EJBException,
    RemoteException
  • public void ejbPassivate() throws EJBException,
    RemoteException
  • public void ejbRemove() throws RemoveException,
    EJBException, RemoteException
  • public void ejbStore() throws EJBException,
    RemoteException

18
Code Home Interface
  • import javax.ejb.CreateException
  • import javax.ejb.EJBHome
  • import javax.ejb.FinderException
  • import java.rmi.RemoteException
  • public interface EmployeeHome extends EJBHome
  • public Employee create( String firstname, String
    lastname, String email ) throws _
    RemoteException, CreateException
  • public Employee findByPrimaryKey( Integer id )
    throws RemoteException, _ FinderException

19
Code Remote Interface
  • import javax.ejb.EJBObject
  • import java.rmi.RemoteException
  • public interface Employee extends EJBObject
  • public void says( String message ) throws
    RemoteException

20
Code ejb-jar.xml
  • ltejb-jargt
  • ltenterprise-beansgt
  • ltentitygt
  • ltejb-namegtEmployeeBeanlt/ejb-namegt
  • lthomegtorg.acme.employee.EmployeeHomelt/homegt
  • ltremotegtorg.acme.employee.Employeelt/remotegt
  • ltejb-classgtorg.acme.employee.EmployeeBeanlt/ejb-
    classgt
  • ltpersistence-typegtContainerlt/persistence-typegt
  • ltprim-key-classgtjava.lang.Integerlt/prim-key-cla
    ssgt
  • ltreentrantgtFalselt/reentrantgt
  • ltcmp-fieldgt
  • ltfield-namegtidlt/field-namegt
  • lt/cmp-fieldgt
  • ltcmp-fieldgt
  • ltfield-namegtfirstnamelt/field-namegt
  • lt/cmp-fieldgt
  • ltcmp-fieldgt
  • ltfield-namegtlastnamelt/field-namegt
  • lt/cmp-fieldgt

21
Code ejb-jar.xml Cont.
  • ltprimkey-fieldgtidlt/primkey-fieldgt
  • ltresource-refgt
  • ltres-ref-namegtjdbc/postgresqllt/res-ref-namegt
  • ltres-typegtjavax.sql.DataSourcelt/res-typegt
  • ltres-authgtContainerlt/res-authgt
  • lt/resource-refgt
  • lt/entitygt
  • lt/enterprise-beansgt
  • ltassembly-descriptorgt
  • ltcontainer-transactiongt
  • ltmethodgt
  • ltejb-namegtEmployeeBeanlt/ejb-namegt
  • ltmethod-namegtlt/method-namegt
  • lt/methodgt
  • lttrans-attributegtSupportslt/trans-attributegt
  • lt/container-transactiongt
  • lt/assembly-descriptorgt
  • lt/ejb-jargt

22
Code Client App
  • import javax.naming.Context
  • import javax.naming.InitialContext
  • import javax.rmi.PortableRemoteObject
  • import java.util.Properties
  • public class EmployeeClient
  • public static void main( String args ) throws
    Exception
  • Properties env new Properties()
  • String jndiProvider "org.openejb.client.Remote
    InitialContextFactory"
  • env.put( "java.naming.factory.initial",
    jndiProvider )
  • env.put( "java.naming.provider.url",
    "localhost4201" )
  • env.put( "java.naming.security.principal",
    "fakeuser" )
  • env.put( "java.naming.security.credentials",
    "fakepass" )
  • Context ctx new InitialContext( env )

23
Code Client App Cont.
  • Object obj ctx.lookup( "EmployeeBean" )
  • obj PortableRemoteObject.narrow( obj,
    EmployeeHome.class )
  • EmployeeHome home ( EmployeeHome ) obj
  • Employee empl_create home.create( "Jacek",
    "Laskowski", "OpenEJB-" System.currentTimeMil
    lis() "_at_SF.net" )
  • Integer primaryKey ( Integer )
    empl_create.getPrimaryKey()
  • Employee empl_find home.findByPrimaryKey(
    primaryKey )
  • System.out.println( "Are the \"create\" and
    \"find\" users identical ? "
    empl_create.isIdentical( empl_find ) )
  • empl_find.says( "Hello OpenEJB World!" )
    empl_find.remove()

24
Code Supporting SQL
  • create table employee
  • (
  • id integer primary key,
  • first_name varchar(15),
  • last_name varchar(15),
  • email varchar(30)
  • )
  • create sequence employee_seq
  • (Code courtesy of http//openejb.codehaus.org/cmp_
    entity_postgresql.html)

25
Conclusion
  • Java Entity Beans are similar to a Java Bean but
    provide an easy way to save data from business
    classes to the database and allows easy
    manipulation of data to reduce coding time.
  • Java Entity Beans have additional components to
    allow for the persistence of data and simplifies
    CRUD for developers.
  • Since Entity beans are instantiated for each
    record there can be performance issues when
    returning a large number of records.

26
Question???
Write a Comment
User Comments (0)
About PowerShow.com