Title: Information Technology KaHo SintLieven
1Entity beans
- Geert De Maere
- GDM_at_cs.Nott.ac.UK
2Overview
- JDBC
- Entity beans
- BMP
- CMP
- Example
- Exercise (e-commerce)
- Guidelines
3JDBC
4JDBC
- Load the driver
- Create a connection
- Create a statement
- Execute the statement
- Use result set
- Cleanup
5Load driver/create connection
java.sql.Connection getConnection() throws
NamingException, SQLException
java.sql.Connection connection null
javax.naming.Context context new
javax.naming.InitialContext()
javax.sql.DataSource ds (javax.sql.DataSour
ce)context.lookup("jdbc/homepage") connection
ds.getConnection() return connection
6Create, execute select, resultset, cleanup
Connection connection null PreparedStatement
ps null ResultSet rs null try
String primaryKey gdm_at_cs.nott.ac.uk
//request connection connection
getConnection() //create statement ps
connection.prepareStatement("SELECT FROM Temail
WHERE email ?") ps.setString(1,
primaryKey) //execute statement rs
ps.executeQuery() //manipulate resultset
while(rs.next())this.name rs.getString("name")
catch(Exception e)throw new
EJBException(e.getMessage()) finally try
//cleanup if(rs!null) rs.close()
if(ps!null) ps.close() if(connection!null)
connection.close() catch(Exception e)throw
new EJBException(e.getMessage())
7Create, execute update, result, cleanup
Connection connection null PreparedStatement
ps null try String primaryKey
gdm_at_cs.nott.ac.uk connection
getConnection() ps connection.prepareStatemen
t("UPDATE Temail SET name ? WHERE email
?") ps.setString(1, name) ps.setString(2,
primaryKey) if(ps.executeUpdate()!1) throw
new EJBException("Failed to update
data") catch(Exception e) throw new
EJBException(e.getMessage()) finally try
if(ps!null) ps.close()
if(connection!null) connection.close()
catch(Exception e) throw new EJBException(e.getM
essage())
8Entity beans(BMP)
9Entity beans
- Implement data access, represents nouns, real
world objects, long lasting data, permanent data,
persistent objects stored in permanent storage,
core business data, view to persistent storage - Persistence mechanisms
- Serialization
- Object-relational mapping (JDBC)
- Object databases
10Entity beans
Entity beans
BMP
CMP
Retrieve data Delete data Add data Update data
Data access logic generated by container-tools
Data access logic written by programmer
Synchronization controlled by the container
11Entity bean types
- Bean managed persistence (BMP)
- Container handles call-back methods
- Programmer is responsible for the data access
logic - Container managed persistence (CMP)
- Container handles persistence
12Why entity beans?
- Objectifying data, simple access
- Reusable
- Middleware provided by the container
- Transactions
- Relations
- Security
-
13Parts of an entity bean
- Interfaces
- Remote
- Remote interface (business)
- Remote home interface (lifecycle, general)
- Local (EJB 2.0)
- Local interface (business)
- Local home interface (lifecycle, general)
- Enterprise bean class
- Deployment descriptor
14Parts of an entity bean
Remotehome interface
Localhome interface
EJB Container
Client
Home Object (factory)
Enterprisebean class
Remote interface
EJB Object Request interception
Local interface
15Remote/local interface (BMP)
- Methods related to one specific bean instance!!!
- Defines business methods
- Setters and getters
- Data logic (example compression)
16Remote/local home interface (BMP)
- Home methods are not related to a specific bean
- Functionality
- Create (remote constructor(s))
- Locate
- Mandatory
- Create-method (remote constructors, throws
CreateException) - findByPrimaryKey-method
- Optional
- Finder-methods (find all records)
- Additional create-methods
- General methods (sum of whole column)
17Entity bean class (BMP)
- Persistence fields
- Must define methods matching the business methods
declared in the remote/local interface - Must define methods matching the methods declared
in the remote/local home interface (prefix ejb
ltmethod-namegt) - Must implement javax.ejb.EntityBean(defines
call-back methods, see next slide) - Implements
- call-back methods
- Lifecycle methods
- Data access logic
18javax.ejb.EntityBean
public interface javax.ejb.EntityBean extends
javax.ejb.EnterpriseBean public void
setEntityContext(javax.ejb.EntityContext)
public void unsetEntityContext() public void
ejbRemove() public void ejbActivate()
public void ejbPassivate() public void
ejbLoad() public void ejbStore()
19Entity bean class, method summary (BMP)
- ejbCreate() insert data
- ejbPostCreate() manage relations
- ejbRemove() delete data
- ejbLoad() select data
- ejbStore() update data
- ejbActivate() acquire resources
- ejbPassivate() release resources
- setEntityContext() pass interface
(entitycontext) to container acquire resources - unsetEntityContext() release entity context
release resources - Business methods
- Home methods
20Bean class, ejbCreate(), ejbRemove() (BMP)
- ejbCreate()
- Insert data
- Must match to create-method in the home interface
- ejbCreate-method returns primary key,
create-method returns EJB object - Acquire resources
- ejbRemove() remove data
- Delete data
- Release resources
21Bean class, ejbLoad(), ejbStore() (BMP)
- Used for synchronisation with the database
- Call-back methods
- ejbLoad() select data
- ejbStore() update data
- Invoked by the container
22Bean class, ejbActivate(), ejbPassivate() (BMP)
- Used for resource pooling
- Call-back methods
- ejbPassivate() release resources
- ejbActivate() acquire resources
- Avoids unnecessary instantiating
- Reduce total amount of beans in memory
23Entity context methods (EJB2.0)
- getEJBLocalObject()
- getEJBObject()
- getPrimaryKey()
24Lifecycle, does not exist
Does not exist
- No instance in memory
- State transitions
- Create instance (1)
- newInstance()
- setEntityContext()
- Delete instance (2)
- unsetEntityContext()
- Garbage collection
1.
2.
Pooled
Ready
25Lifecycle, pooled state
Does not exist
- Not bound to any specific data
- Functionality
- Finder methods (1)
- ejbHome-methods (2)(general methods)
- State transitions
- Delete instance (3)
- Create instance (4)
- ejbCreate()
- ejbPostCreate()
- Activate instance (5)
- ejbActivate()
- ejbLoad()
3.
Pooled
1.
2.
5.
4.
Ready
26Lifecycle, ready state
Does not exist
- Bound to dataset
- Transitions
- Remove instance (1)(ejbRemove())
- Passivate instance (2)
- ejbStore()
- ejbPassivate()
- Methods
- Business method-calls
- ejbLoad() (3)
- ejbStore() (4)
Pooled
1.
2.
Ready
3.
4.
27Example, email-name table(BMP)
28Example, diagram
ltltinterfacegtgt Remote (java.rmi)
ltltinterfacegtgt Serializable (java.io)
J2EE platform
ltltinterfacegtgt EnterpriseBean (javax.ejb)
EJB distribution
ltltinterfacegtgt EJBObject (javax.ejb)
ltltinterfacegtgt EJBHome (javax.ejb)
ltltinterfacegtgt EJBLocalHome (javax.ejb)
ltltinterfacegtgt EntityBean (javax.ejb)
ltltinterfacegtgt EmailData
ltltinterfacegtgt EmailData- Home
ltltinterfacegtgt EmailData- LocalHome
EntityBean
Write yourself
Remote EJBObject
RemoteEJBHome Obj
Local EJB Home obj
Generated by container tools
29Example, remote interface
package example import javax.ejb. import
java.rmi. public interface EmailData extends
EJBObject public void setName(String name)
throws RemoteException public String getName()
throws RemoteException public String
getEmail() throws RemoteException
30Example, local interface
package example import javax.ejb. public
interface EmailDataLocal extends
EJBLocalObject public void setName(String
name) public String getName() public String
getEmail()
31Example, remote home interface
package example import javax.ejb. import
java.rmi. public interface EmailDataHome
extends EJBHome public EmailData
create(String email, String name) throws
RemoteException, CreateException public
EmailData findByPrimaryKey(String email)
throws RemoteException, FinderException
32Example, local home interface
package example import javax.ejb. public
interface EmailDataLocalHome extends
EJBLocalHome public EmailDataLocal
create(String email, String name) throws
CreateException public EmailDataLocal
findByPrimaryKey(String email) throws
FinderException
33Example, bean class
package example import javax.ejb. import
javax.naming. import javax.sql. import
java.sql. public class EmailDataBean
implements javax.ejb.EntityBean private
java.lang.String email null private
java.lang.String name null private
EntityContext ctx null public String
ejbCreate(String email, String name) throws
CreateException /code to insert data/
public void ejbPostCreate(String email, String
name) /manage relationships/ public void
ejbRemove() /code to remove data/ . . .
34Example, bean class
. . . public void ejbLoad() /code to
select data/ public void ejbStore()
/code to update data/ public void
ejbActivate() /acquire resources/ public
void ejbPassivate() /release resources/
public String ejbFindByPrimaryKey(String email)
throws FinderException /locate data/
public void setEntityContext(EntityContext
entityContext) this.ctx entityContext
public void unsetEntityContext() this.ctx
null . . .
35Example, bean class
. . . public void setName(String name)
this.name name public String getName()
return this.name public String getEmail()
return this.email Connection
getConnection() throws NamingException,
SQLException /see jdbc slides/
36Example, ejbLoad()
. . . try String primaryKey (String)
ctx.getPrimaryKey() connection
getConnection() ps connection.prepareState
ment("SELECT name FROM Temail WHERE email
?") ps.setString(1, primaryKey) rs
ps.executeQuery() if(rs.next()) this.name
rs.getString("name") catch(Exception e)throw
new EJBException(e.getMessage()) finally try
if(rs!null) rs.close() if(ps!null)
ps.close() if(connection!null)
connection.close() catch(Exception e)throw
new EJBException(e.getMessage())
37Example, ejbCreate()
try this.name name this.email email
connection getConnection() ps
connection.prepareStatement("INSERT INTO Temail
(email, name) VALUES (?, ?)") ps.setString(1,
email) ps.setString(2, name)
if(ps.executeUpdate()!1) throw new
CreateException("Failed to insert
data") catch(Exception e) throw new
EJBException(e.getMessage()) finally try
if(ps!null) ps.close() if(connection!null)
connection.close() catch(Exception e)
throw new EJBException(e.getMessage())
return this.email
38Naming conventions
- Access methods (remote/local interface)
- setltPersistence-fieldgt
- getltPersistence-fieldgt
- Files
- Remote interface ltlogical-object-namegt
- Remote interface lt logical-object-namegt Home
- Local interface lt logical-object-namegt Local
- Local home interface lt logical-object-namegt
LocalHome - Bean class lt logical-object-namegt Bean
39Remark, finder methods (BMP)
- Used for lookups
- All finder methods must begin with ejbFind
- ejbFindByPrimaryKey is mandatory
- Return value
- Primary key
- Collection of primary keys
- Must match finder methods in the home interface
- Must throw finder-exception
- Associated EJB objects are created!!!
40Exercise
41Example, deployment descriptor
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD
Enterprise JavaBeans 2.0//EN' 'http//java.sun.com
/dtd/ejb-jar_2_0.dtd'gt ltejb-jargt
ltdisplay-namegtemailbeanEJBlt/display-namegt
ltenterprise-beansgt ltentitygt
ltdisplay-namegtEmailDataBeanlt/display-namegt
ltejb-namegtEmailDataBeanlt/ejb-namegt
lthomegtexample.EmailDataHomelt/homegt
ltremotegtexample.EmailDatalt/remotegt
ltlocal-homegtexample.EmailDataLocalHomelt/local-home
gt ltlocalgtexample.EmailDataLocallt/localgt
ltejb-classgtexample.EmailDataBeanlt/ejb-classgt
ltpersistence-typegtBeanlt/persistence-typegt
ltprim-key-classgtjava.lang.Stringlt/prim-key-classgt
ltreentrantgtFalselt/reentrantgt
ltsecurity-identitygt ltdescriptiongtlt/descrip
tiongt ltuse-caller-identitygtlt/use-caller-id
entitygt lt/security-identitygt
42Example, deployment descriptor
. . . ltresource-refgt
ltres-ref-namegtjdbc/homepagelt/res-ref-namegt
ltres-typegtjavax.sql.DataSourcelt/res-typegt
ltres-authgtContainerlt/res-authgt
ltres-sharing-scopegtShareablelt/res-sharing-scopegt
lt/resource-refgt lt/entitygt
lt/enterprise-beansgt ltassembly-descriptorgt
ltmethod-permissiongt ltunchecked /gt
ltmethodgt ltejb-namegtEmailDataBeanlt/ejb-name
gt ltmethod-intfgtLocallt/method-intfgt
ltmethod-namegtgetPrimaryKeylt/method-namegt
ltmethod-params /gt lt/methodgt . . .
lt/method-permissiongt lt/assembly-descriptorgt lt/e
jb-jargt
43Example, client
package example import javax.naming. import
javax.rmi.PortableRemoteObject public class
EmailDataClient public static void
main(String arg) try Context ctx
new InitialContext() Object o
ctx.lookup("ejb/emailbean")
example.EmailDataHome home (example.EmailDataHom
e) javax.rmi.PortableRemoteObject.narrow(o,
example.EmailDataHome.class)
example.EmailData em (example.EmailData) home.cr
eate("gdm_at_cs.nott.ac.uk", "Geert De Maere")
em.setName("G. De Maere")
System.out.println("Name " em.getName())
System.out.println("Email " em.getEmail())
catch(Exception e)
System.out.println("Exception "
e.getMessage()) e.printStackTrace()
44BMP Conclusion
- Advantages
- Flexibilty
- Alternative if CMP is inadequate
- Disadvantages
- Hard to write, persistence is implemented by
the bean programmer - ejbCreate()
- ejbLoad()
- ejbStore()
- ejbRemove()
- Persistence code is database dependent
45Entity BeansCMP (EJB2.0)
46Entity beans
Entity beans
BMP
CMP
Retrieve data Delete data Add data Update data
Data access logic generated by container-tools
Data access logic written by programmer
Synchronization controlled by the container
47CMP History
- EJB 1.0 standards
- EJB 1.1 perfecting standards
- EJB 2.0
- Support for complex data-types
- Relationships
- Virtual persistence fields (abstract accessor
methods) - EJB-QL
48Generating persistence classes
Abstract programming model (Bean class)
Abstract persistence schema (deployment
descriptor)
Container tools
Database access logic is generated at deployment
time by container tools
49Abstract programming model
- Attributes and relations are described as virtual
fields using abstract accessor methods(implementa
tion is deferred to deployment time/persistence
class) - Declared as abstract, not implemented by the bean
provider, though code is generated by the
container tools (in contrast to BMP)
50Abstract persistence schema
- set of XML elements in the deployment
descriptor that describes the relationship fields
and the persistence fields
51Parts of an entity bean
- Interfaces
- Remote
- Remote interface (business)
- Remote home interface (lifecycle, general)
- Local (EJB 2.0)
- Local interface (business)
- Local home interface (lifecycle, general)
- Enterprise bean class
- Deployment descriptor
52Remote/local interface (BMP)
- Methods related to one specific bean instance!
- Defines business methods used by the client to
interact with the bean (client view) - Setters and getters
- Data logic (example compression)
- (Matching methods are defined in the bean class)
- Must NOT match relationship fields
53Remote/Local home interface (BMP)
- Home methods are not related to a specific bean!
- Functionality
- Create
- locate
- Mandatory
- Create-method(Remote constructors, suffixes
allowed (createByName(), throws
javax.ejb.CreateException) - findByPrimaryKey-method (not implemented in the
bean class!!!) - Optional
- Additional create-methods
- Finder-methods(find all records)(not implemented
in the bean class, implemented in the container
generated persistence class, based on the EJB-QL
query in the deployment descriptor) - General methods (sum of whole column)
54Entity bean class(!BMP)
- Used by the container to generate persistence
class - Abstract class not deployed!!!
- Implement javax.ejb.EntityBean
- Contains
- Abstract accessor methods (not implemented!)
- Persistence fields
- Relationship fields
- ejbSelect() methods (not exposed to clients)
- Provides the implementation of business methods
- No implementation of finder- and select methods
(findByPrimaryKey). They are generated by the
container in the persistence class
55Entity bean class, method summary(CMP)
- Abstract accessor methods (persistence fields in
BMP) - ejbCreate() set data using abstract methods
- ejbPostCreate() manage relations, empty method
(implemented by container) - ejbRemove() delete data, empty method
(implemented by container) - ejbLoad() select data, empty method (implemented
by container) - ejbStore() update data, empty method
(implemented by container) - ejbActivate() acquire resources
- ejbPassivate() release resources
- setEntityContext() acquire context resources
- unsetEntityContext() release context resources
- ejbFindByPrimaryKey() not defined!
- (ejbSelect() abstract helper methods!)
- Does not define finder methods from the home
interface! - Business methods
56Entity bean class, naming conventions
- ltfieldNamegt starts with lower case
- getltFieldNamegt
- First letter of fieldname is capitalised
- Return type same type of persistence field
- setltFieldNamegt
- First letter of fieldname is capitalised
- Parameter type same type as persistence field
Conventions are mandatory!!!
57Persistence class
- Generated by the container
- Includes
- Persistence fields
- Get and set methods
- Data logic
58Deployment descriptor
- Describes the bean
- EJB-name
- Classes
- Security
- Transactions
- Persistence type (Container, Bean)
- EJB version
- . . .
- Describes the abstract persistence schema
- CMP version
- Persistence fields
- Primary key
- Class
- field
59Example, email-name table(CMP)
60Example, diagram
ltltinterfacegtgt Remote (java.rmi)
ltltinterfacegtgt Serializable (java.io)
J2EE platform
ltltinterfacegtgt EnterpriseBean (javax.ejb)
EJB distribution
ltltinterfacegtgt EJBObject (javax.ejb)
ltltinterfacegtgt EJBHome (javax.ejb)
ltltinterfacegtgt EJBLocalHome (javax.ejb)
ltltinterfacegtgt EntityBean (javax.ejb)
ltltinterfacegtgt EmailData
ltltinterfacegtgt EmailData- Home
ltltinterfacegtgt EmailData- LocalHome
EmailData- Bean (abstract)
Write yourself
Remote EJBObject
RemoteEJBHome Obj
Local EJB Home obj
Generated by container tools
Persistence_ EmailData- Bean
61Entity bean classes
Entity beans
BMP
CMP
ltltinterfacegtgt java.io.Serializable
ltltinterfacegtgt java.io.Serializable
J2EE platform
ltltinterfacegtgt javax.ejb.EnterpriseBean
ltltinterfacegtgt javax.ejb.EnterpriseBean
EJB distribution
ltltinterfacegtgt javax.ejb.EntityBean
ltltinterfacegtgt javax.ejb.EntityBean
Write yourself
EmailDataBean
EmailDataBean
Generated by container tools
Persistence_EmailDataBean
62Example, remote interface
package example import javax.ejb. import
java.rmi. public interface EmailData extends
EJBObject public void setName(String name)
throws RemoteException public String getName()
throws RemoteException public String
getEmail() throws RemoteException
63Example, local interface
package example import javax.ejb. public
interface EmailDataLocal extends
EJBLocalObject public void setName(String
name) public String getName() public String
getEmail()
64Example, remote home interface
package example import javax.ejb. import
java.rmi. public interface EmailDataHome
extends EJBHome public EmailData
create(String email, String name) throws
RemoteException, CreateException public
EmailData findByPrimaryKey(String email)
throws RemoteException, FinderException
65Example, local home interface
package example import javax.ejb. public
interface EmailDataLocalHome extends
EJBLocalHome public EmailDataLocal
create(String email, String name) throws
CreateException public EmailDataLocal
findByPrimaryKey(String email) throws
FinderException
66Example, bean class
package EmailDataCMP import javax.ejb. public
abstract class EmailDataBean implements
javax.ejb.EntityBean protected EntityContext
ctx public String ejbCreate(String email,
String name) throws CreateException
setEmail(email) setName(name) return
null public void ejbPostCreate(String email,
String name) public abstract String
getEmail() public abstract void setEmail(String
email) public abstract String getName()
public abstract void setName(String name) .
. .
67Example, bean class
. . . public void ejbActivate() public void
ejbLoad() public void ejbPassivate() public
void ejbRemove() public void ejbStore()
public void setEntityContext (javax.ejb.EntityCo
ntext entityContext) this.ctx
entityContext public void unsetEntityContext()
this.ctx null
68Deployment descriptor
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD
Enterprise JavaBeans 2.0//EN' 'http//java.sun.com
/dtd/ejb-jar_2_0.dtd'gt ltejb-jargt
ltdisplay-namegtEmailDataEJBlt/display-namegt
ltenterprise-beansgt ltentitygt
ltdisplay-namegtEmailDataBeanlt/display-namegt
ltejb-namegtEmailDataBeanlt/ejb-namegt
lthomegtEmailDataCMP.EmailDataHomelt/homegt
ltremotegtEmailDataCMP.EmailDatalt/remotegt
ltlocal-homegtEmailDataCMP.EmailDataLocalHomelt/local
-homegt ltlocalgtEmailDataCMP.EmailDataLocallt/l
ocalgt ltejb-classgtEmailDataCMP.EmailDataBeanlt
/ejb-classgt ltpersistence-typegtContainerlt/per
sistence-typegt ltprim-key-classgtjava.lang.Str
inglt/prim-key-classgt ltreentrantgtFalselt/reent
rantgt ltcmp-versiongt2.xlt/cmp-versiongt
ltabstract-schema-namegtEmailDataCMPlt/abstract-schem
a-namegt . . .
69Deployment descriptor
ltcmp-fieldgt ltdescriptiongtno
descriptionlt/descriptiongt
ltfield-namegtnamelt/field-namegt lt/cmp-fieldgt
ltcmp-fieldgt ltdescriptiongtno
descriptionlt/descriptiongt
ltfield-namegtemaillt/field-namegt
lt/cmp-fieldgt ltprimkey-fieldgtemaillt/primkey-f
ieldgt . . . lt/entitygt
lt/enterprise-beansgt . . .
70Deployment descriptor
ltassembly-descriptorgt ltcontainer-transactiongt
ltmethodgt ltejb-namegtEmailDataBeanlt/ejb-
namegt ltmethod-intfgtHomelt/method-intfgt
ltmethod-namegtremovelt/method-namegt
ltmethod-paramsgt ltmethod-paramgtjava.lang.O
bjectlt/method-paramgt lt/method-paramsgt
lt/methodgt lttrans-attributegtRequiredlt/trans-at
tributegt lt/container-transactiongt . . .
lt/assembly-descriptorgt lt/ejb-jargt
71Example, Client
package EmailDataCMP import javax.naming. impor
t javax.rmi. public class EmailClient public
static void main(String arg) try Context
ctx new InitialContext() Object o
ctx.lookup("javacomp/env/ejb/emailbean")
EmailDataCMP.EmailDataHome home
(EmailDataCMP.EmailDataHome)
javax.rmi.PortableRemoteObject.narrow(o,
EmailDataCMP.EmailDataHome.class)
mailDataCMP.EmailData em (EmailDataCMP.EmailData
) home.create("gdm_at_cs.nott.ac.uk", "Geert De
Maere") em.setName("G. De Maere")
System.out.println("Name " em.getName())
System.out.println("Email " em.getEmail())
catch(Exception e)
System.out.println("Exception "
e.getMessage())
72EJB Query Language(EJB QL)
73EJB Query Language (EJB QL)
- Standardised SQL-like language used by the
container to define how custom find/select
(query) methods work - EJB QL is translated to the data stores native
language - EJB QL makes queries portable across different
EJB servers
74Query methods
75Declaring queries
ltquerygt ltquery-methodgt ltmethod-namegtfindByNa
melt/method-namegt ltmethod-parmsgt
ltmethod-paramgtjava.lang.Stringlt/method-paramgt
lt/method-paramsgt lt/query-methodgt ltejb-qlgt
SELECT OBJECT(e) FROM email e WHERE e.name ?1
lt/ejb-qlgt lt/querygt
76EJB-QL
SELECT OBJECT(e) FROM email as e SELECT e.name
FROM email as e SELECT DISTINCT OBJECT(e) FROM
email as e SELECT OBJECT(e) FROM email as e
WHERE e.name Geert De Maere SELECT
OBJECT(e) FROM email as e WHERE e.name ?1
77WHERE-Operators
- Navigation operator (.)
- Arithmetic operators
- , - (unary)
- , / (multiplication and division)
- , - (addition and subtraction)
- Comparison operators(lt, lt, gt, gt, , ltgt)
- Logical operators(not, and, or)
78EJB-QL shortcomings
- OBJECT-operator
- No ORDER-BY operator
- Bad support for DATE types
- Limited functional expression (sum, count, avg,
min, max, )
79Exercise
80Generating persistence classes
Abstract programming model (Bean class)
Abstract persistence schema (deployment
descriptor)
Container tools
Database access logic is generated at deployment
time by container tools
81CMP relations
82CMP conclusion
- Advantages
- Easy to use
- Rapid application development/prototyping
- Improved performance
- Clear separations of logic and access code, soft
coded SQL - Relations are easy to use
- Disadvantages
- Hard to debug
- Lack of control
- Complexity of mappings depends on the container
83Guidelines
84Entity beans vs. direct access
- ENTITY BEANS
- Automatic synchronisation
- Object oriented
- Pass by reference
- Remote
- Local(wrapping beans)
- Middle tier caching(reduce database traffic)
- Single data access point
- Rapid application development (when using tools)
- DIRECT ACCESS
- Manual synchronisation
- Procedural
- Pass by value
- Database caching
- Distributed data access
- Slow development
85Wrapping entity beans
EJB Container
Client
1. findAll()
2. ejbFindAll()
Home Object (factory)
3. Remote refs
3. Primary keys
EJB Object Request interception
Pass by Reference
86Wrapping entity beans
EJB Container
Client
1. findAll()
2. ejbFindAll()
Home Object (factory)
3. By value
3. Wrapper obj
findAll
Local refs
Pass by value session bean is used as facade
87Using stored procedures
- Advantages
- Data intensive operations(avoid sending all data
over the network) - Shared business rules
- Common ejb layer
- Database
- Security
- Centralized and optimised SQL
- Precompiled (PreparedStatements)
- Disadvantages
- Database server often becomes bottleneck
- Difficult to maintain (ejb and database knowledge
necessary) - Difficult to port to other database, written in
proprietary language
88Stored procedures, JDBC
// Define the code to invoke a stored
function CallableStatement orderCounter
connection.prepareCall("call ?
COUNT_CUSTOMER_ORDERS(?)" ) // Invoke the
stored function orderCounter.registerOutParameter(
1, java.sql.Types.FLOAT) orderCounter.setInt(2,
customer.getCustomerID() ) orderCounter.execute()
// Get the return value numberOfOrders
orderCounter.getFloat(2) // End the transaction
and close the connection connection.commit() orde
rCounter.close()
89Optimise performance
- Call entity beans trough co-located session beans
- Use caching
- Delay synchronisation