Transcript EJB 9

Collaborate
Knowledge Byte
In this section, you will learn about:
•
•
Cascading deletes in entity beans
Undefined primary key in entity bean
Collaborate
Lesson 3C / Slide 1 of 18
Collaborate
Cascading Deletes in Entity Beans
•
•
•
•
•
•
•
When you invoke the remove() method of a CMP entity bean’s EJB object, the
container calls the corresponding ejbRemove() method of the bean
implementation class.
The ejbRemove() method is responsible for deleting data associated with the
bean instance.
EJB container first removes a CMP entity bean from various relationships and
then deletes the bean and its associated data.
You can delete an entity bean and the beans related to it, simultaneously. This
involves initializing a series of deletions, starting with the specified CMP entity
bean.
The deletion continues till all the related entity beans have also been
successfully removed. This technique is called cascading delete.
Cascading delete is performed for one-to-one and one-to-many relationships
between the entity beans.
You can use the cascading delete technique when the other end of relationship
has multiplicity 1.
Collaborate
Lesson 3C / Slide 2 of 18
Collaborate
Cascading Deletes in Entity Beans
(Contd.)
•
•
The cascading delete cannot be implemented for *-* relationships. This is
because in case of many-to-many relationships the beans are not related
exclusively to each other.
You perform the cascading delete operations by declaring the <cascadedelete/> tag in the <ejb-relationship-role> tag of the deployment
descriptor of a CMP entity bean.
Collaborate
Lesson 3C / Slide 3 of 18
Collaborate
Undefined Primary Key in Entity
Beans
•
•
•
•
•
•
You can define the primary key of a CMP entity bean at the time of its
deployment. This technique is used when you do not know which field needs to
be declared as a primary key at the time of coding an enterprise bean.
Using this technique, you can specify primary key of the enterprise bean at
deployment time.
As a result, you can declare the primary key according to the type of database
on which the bean is being deployed.
The argument type of the findByPrimaryKey() method is declared as
java.lang.Object when you declare primary key of an entity bean at
deployment time.
You need to declare the type of primary key class as java.lang.Object in the
deployment descriptor.
You also need to avoid accessing the primary key in the CMP entity bean
methods because the primary key is only defined at deployment time.
Collaborate
Lesson 3C / Slide 4 of 18
Collaborate
From the Expert’s Desk
In this section, you will learn:
•
•
•
Best Practice on:
• Tuning Entity Beans
Tips and Tricks on:
• Lazy Loading of Entity Bean References
• When to Use Entity Beans
FAQs on CMP and EJB-QL
Collaborate
Lesson 3C / Slide 5 of 18
Collaborate
Best Practices
Tuning Entity Beans
•
You can optimize the performance of entity beans by using the following
techniques:
• Using setEntityContext() as cache
•
•
•
Committing data after completing a transaction
Specifying entity beans instance cache size
Using connection pool
Collaborate
Lesson 3C / Slide 6 of 18
Collaborate
Tips and Tricks
Lazy Loading of Entity Bean References
•
Lazy loading:
• Refers to the process of delaying the loading of an entity bean’s related
beans until the entity bean references them.
• Provides minimal overhead because it loads a related entity bean only
when it is required
• Is performed by the bean provider in BMP by providing code for it.
• Is managed by EJB container in CMP beans. However, in CMP you need
to enable this feature in the container in order to use it.
Collaborate
Lesson 3C / Slide 7 of 18
Collaborate
Tips and Tricks
When to Use Entity Beans
•
The following techniques are used to store the data of an EJB application,
and make it persistent:
• Using session beans with JDBC
• Using entity beans
Collaborate
Lesson 3C / Slide 8 of 18
Collaborate
Tips and Tricks
When to Use Entity Beans (Contd.)
•
The following table compares the two types of persistence implementations
in EJB applications:
Parameter for
Comparison
Control
Session beans with
JDBC
Entity Beans
In session beans, bean
provider has control
over the connectivity
because the code for
connectivity is written
and managed by the
bean provider himself.
In entity beans, EJB
container manages the
connectivity. The bean
provider does not have much
control over the connectivity
and database access.
Collaborate
Lesson 3C / Slide 9 of 18
Collaborate
Tips and Tricks
When to Use Entity Beans (Contd.)
•
Comparison between the two types of persistence implementations: (Contd.)
Parameter
for
Comparison
Session beans with JDBC
Comparison
Procedural
versus
objectoriented
Session beans perform
business operations and return
the data as result sets that use
the procedural approach.
Entity beans use the object-oriented
approach and are used to represent
the Java objects that can support
encapsulation and relationships.
Caching
Session beans represent only
the business operations and
they cannot be used for
caching the data.
The entity beans represent the data
in the database. Therefore, the
caching of data can be performed
for the entity beans.
Collaborate
Lesson 3C / Slide 10 of 18
Collaborate
Tips and Tricks
When to Use Entity Beans (Contd.)
•
Comparison between the two types of persistence implementations: (Contd.)
Parameter
for
Comparison
Session beans with JDBC
Comparison
Migration
Session beans use directly or
slightly modified SQL code for
JDBC connectivity and
database operations.
Entity beans use the objectoriented approach and they require
modifications in SQL code to
implement the connectivity.
Development
time
In case of session beans, you
need to write the JDBC code
for database connectivity and
operations.
In CMP entity beans, the code for
database operations and
connectivity is easily created and
the development time is faster than
the session beans with JDBC.
Collaborate
Lesson 3C / Slide 11 of 18
Collaborate
FAQs
•
What are the advantages of CMP entity beans as compared to BMP entity
beans?
CMP entity beans have the following advantages as compared to BMP entity
beans:
• CMP beans are easier to write because you do not write the JDBC code.
As a result, you can concentrate on implementing the business logic of
application.
• CMP beans are more portable than BMP entity beans as BMP entity
beans contain database-specific connectivity code.
• CMP beans provide an easier and simpler way to define the various
relationships with other entity beans. You can specify and select the
relationships in the GUI of the deployment tool. In a BMP entity bean,
you have to write the code to implement and manage the relationships.
Collaborate
Lesson 3C / Slide 12 of 18
Collaborate
FAQs (Contd.)
•
What are the finder methods in EJB?
The finder methods, declared in the home interface of an enterprise bean,
are used by the clients to locate the entity beans. The finder methods are
declared as starting with findBy prefix. The argument of a finder method is
the field value based on which the entity bean object will be retrieved. The
example of the finder methods are findByPrimaryKey() and findByName().
•
You need to import which interface while creating a CMP bean?
You need to import the javax.ejb.EntityBean interface.
Collaborate
Lesson 3C / Slide 13 of 18
Collaborate
FAQs (Contd.)
•
What are the three main clauses of an EJB-QL query where path expressions
can appear?
The three clauses are, SELECT, FROM, and WHERE.
•
What are the three types of literals used in EJB-QL?
The three types of literals used in EJB QL are, string, numeric, and boolean.
Collaborate
Lesson 3C / Slide 14 of 18
Collaborate
Challenge
1.
2.
3.
4.
5.
In CMP entity beans, the ____________ manages the persistence of the
enterprise bean.
The _____________ method is used to release the resources held by entity
bean instance before the bean instance is sent to the Passivate stage.
The __________ tag in the deployment descriptor specifies the type of
persistence in entity beans.
The __________ path expression is used to find whether an expression’s
value falls within the specified range or not.
The instantiated and unused instances of an enterprise bean are stored in
_____________.
Collaborate
Lesson 3C / Slide 15 of 18
Collaborate
Challenge (Contd.)
6.
The method(s) that make(s) an entity bean instance eligible for garbage
collection is(are):
A. ejbRemove()
B. unsetEntityContext()
C. finalize()
a.
b.
c.
d.
Only A is true
Both, B and C, are true
Only C is true
Both, A and C, are true
Collaborate
Lesson 3C / Slide 16 of 18
Collaborate
Challenge (Contd.)
7.
Who is responsible for providing the tools for reading and importing the
information contained in the deployment descriptor and implementing the
tasks of a container:
a.
Bean Provider
b.
Application Assembler
c.
Container Provider
d.
End User
Collaborate
Lesson 3C / Slide 17 of 18
Collaborate
Solutions to Challenge
1.
2.
3.
EJB container
ejbPassivate()
<persistence-type>
4.
5.
6.
7.
BETWEEN
Shared pool
c. Only C is true
c. Container Provider
Collaborate
Lesson 3C / Slide 18 of 18