Transcript EJB 6
Collaborate
Knowledge Byte
In this section, you will learn about:
•
•
Declaring relationships in BMP entity beans
Object-to-relational mapping in EJB
Collaborate
Lesson 2C / Slide 1 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans
•
•
The bean developer writes the code for defining and managing relationships
between BMP entity beans.
Various types of the relationships that can exist between BMP entity beans are:
• One-to-One Relationships
• One-to-Many Relationships
• Many-to-Many Relationships
Collaborate
Lesson 2C / Slide 2 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans (Contd.)
•
•
One-to-One Relationships:
• Specify that each instance of an entity bean is related to a single instance
of another entity bean.
• For example, in a banking application, each account may only have one
particular loan associated with it. This means that the relationship between
the Account and Loan entity beans is one-to-one.
One-to-Many Relationships:
• Specify that each instance of an entity bean is related to several instances
of another entity bean.
• For example, in a banking application, the relationship between the
Customer and Account entity beans is one-to-many. This means that a
customer can have more than one account in a bank but one account can
belong to only one customer.
Collaborate
Lesson 2C / Slide 3 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans (Contd.)
•
Many-to-Many Relationships:
• Specify that multiple instances of an entity bean are related to several
instances of another entity bean.
• For example, in a banking application, an account can be shared between
multiple customers. In addition, one customer can have several accounts.
As a result, the relationship between the Account and the Customer entity
beans is many-to-many.
• Can either be:
• Fake many-to-many relationship
• True many-to-many relationship
Collaborate
Lesson 2C / Slide 4 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans (Contd.)
•
•
Fake many-to-many relationship:
• Is implemented by using an association table.
• For example, you can create the CustomerAccount table as an
association table linking the Customer and Account tables.
True many-to-many relationship:
• Is implemented using the ejbLoad() method, which invokes a finder
method by using the home interface of the other entity bean in the
relationship. The finder method uses the primary key of the other
entity bean as an argument.
Collaborate
Lesson 2C / Slide 5 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans (Contd.)
•
Directionality:
• It refers to the direction in which you can navigate a relationship.
• Relationships can be:
• Unidirectional
• Bi-directional
Collaborate
Lesson 2C / Slide 6 of 22
Collaborate
Declaring Relationships in BMP Entity
Beans (Contd.)
•
•
Unidirectional relationship:
• Specifies that only one BMP entity bean consists of the get and set
methods and a field that references the other BMP entity bean. For
example, in a unidirectional relationship between two entity beans
A and B, a client can only navigate from A to B and not from B to
A.
Bi-directional relationship:
• Specifies that each BMP entity bean consists of one field pointing to
the other BMP entity bean in the relationship. For example, in a bidirectional relationship between two entity beans A and B, a client
can navigate from A to B and from B to A.
• Specifies that each entity bean that participates in the bidirectional relationships consists of the get and set methods.
Collaborate
Lesson 2C / Slide 7 of 22
Collaborate
Object-to-Relational Mapping in EJB
•
Object-to-relational mapping:
• Is the method of mapping an object to a relational database.
• Enables you to store Java objects in a relational database, such as
SQL Server or Oracle.
• Enables you to issue queries for retrieving particular information.
• Can be performed using the following two methods:
• By providing the code in your application using a database
access API, such as JDBC.
• By using an object-relational mapper, such as Sun's JavaBlend.
Collaborate
Lesson 2C / Slide 8 of 22
Collaborate
From the Expert’s Desk
In this section, you will learn:
•
•
•
Best Practice on:
• Choosing Between HTTP Session Object and Stateful Session Bean
to Maintain Client State
Tips and Tricks on:
• Optimizing the Performance of Stateful Session Beans
• Optimizing the Use of JDBC in BMP Entity Beans
FAQs on session beans
Collaborate
Lesson 2C / Slide 9 of 22
Collaborate
Best Practices
Choosing Between HTTP Session Object and
Stateful Session Bean to Maintain Client State
•
•
While interacting with an enterprise application, the operation performed by
the client during a session may require storing some client-specific
information.
The following methods are used to maintain the client state in an application:
• HTTP Session Object
• Stateful Session Beans
Collaborate
Lesson 2C / Slide 10 of 22
Collaborate
Best Practices
Choosing Between HTTP Session Object and
Stateful Session Bean to Maintain Client State
(Contd.)
•
HTTP Session Object:
• Is a simple object that can be used for storing client-specific
information for a particular session.
• Has the following advantages:
• Requires less resources for maintaining the state in a client.
• Requires less time in maintaining the client state.
• Enables the clients using Servlet and JSP to maintain the
client state easily.
Collaborate
Lesson 2C / Slide 11 of 22
Collaborate
Best Practices
Choosing Between HTTP Session Object and
Stateful Session Bean to Maintain Client State
(Contd.)
•
The HTTP Session Object has the following disadvantages:
• Client state is lost if a client restarts or logs-out from the Web
browser.
• Client is allowed to change its system while accessing an enterprise
bean.
Collaborate
Lesson 2C / Slide 12 of 22
Collaborate
Best Practices
Choosing Between HTTP Session Object and
Stateful Session Bean to Maintain Client State
(Contd.)
•
Stateful Session Beans:
• Can be used to maintain the client state.
• Store the conversational state of an individual client with the
enterprise bean.
• For example, in an online bookshop application, a stateful session
bean is used for storing the names of books selected by a
customer. The names stored are retained even if the client calls
other methods of the bean.
Collaborate
Lesson 2C / Slide 13 of 22
Collaborate
Best Practices
Choosing Between HTTP Session Object and
Stateful Session Bean to Maintain Client State
(Contd.)
•
Stateful Session Beans have the following advantages:
• Enable the Web-based and non-Web-based clients to store clientspecific information.
• Improve scalability of an application by using a shared pool to
store the bean instances.
• Provide support for transactions, security services, and RMI.
• Allow a client to perform multiple operations in a single HTTP client
request.
Collaborate
Lesson 2C / Slide 14 of 22
Collaborate
Tips and Tricks
Optimizing the Performance of Stateful Session
Beans
•
The measures to optimize the performance of a stateful session bean are:
• Define an optimal cache size
• Define an optimal time out value for each bean instance
• Remove a stateful session bean, explicitly
• Declare transient variables
Collaborate
Lesson 2C / Slide 15 of 22
Collaborate
Tips and Tricks
Optimizing the Use of JDBC in BMP Entity Beans
•
•
You use the JDBC packages, java.sql and javax.sql, to connect to a
database.
JDBC provides a connection pool that:
• Consists of several open database connections. You can specify the
minimum and maximum number of open connections in the connection
pool.
• Helps in optimizing performance, as you do not require establishing a
new connection with the database.
Collaborate
Lesson 2C / Slide 16 of 22
Collaborate
Tips and Tricks
Optimizing the Use of JDBC in BMP Entity Beans
(Contd.)
•
When you use JDBC in the BMP entity beans, the following criteria should be
considered:
• Use batch transaction in order to execute multiple statements on a
connection.
• Use the PreparedStatement object to execute a statement multiple
times.
• Retrieve data from a database iteratively, in small amounts, instead of
retrieving the entire data at one go.
• Store the read-only and read-mostly data from a table in a cache.
• Use the batch update feature of the statement object when you need to
issue several queries to a database simultaneously.
• Specify the appropriate isolation level according to the requirements of
the application.
Collaborate
Lesson 2C / Slide 17 of 22
Collaborate
Tips and Tricks
Optimizing the Use of JDBC in BMP Entity Beans
(Contd.)
•
•
You need to choose the right type of driver to optimize the use of JDBC driver
in the BMP entity beans.
When selecting a driver, the following criteria should be considered:
• Use the Native protocol - All - Java driver while communicating with a
database using an applet.
• Use the Native API - Partly - Java driver while using a Java client to
communicate with a database.
• Use the JDBC-ODBC driver if the database with which you establish a
connection does not support any driver.
• Use the JDBC - Net - All Java driver for establishing communication
between a client and a proxy server when the application is threetiered.
Collaborate
Lesson 2C / Slide 18 of 22
Collaborate
FAQs
•
How does a stateful session bean maintain the client conversational state?
A stateful session bean stores the values of variables in a secondary storage
while passivating the bean instance associated with a client.
•
What is the purpose of the Passive stage in a stateful session bean life cycle?
In the Passive stage, EJB Container enables an idle stateful session bean
instance to service another client’s request.
Collaborate
Lesson 2C / Slide 19 of 22
Collaborate
FAQs (Contd.)
•
Is it necessary to override the ejbStore() and ejbLoad() methods in a BMP
entity bean application if there are no delete and update options?
The ejbStore() and ejbLoad() methods are used to synchronize the bean
state with a database. For this reason, it is necessary to implement and
override these methods.
•
What is the use of ejbPostCreate() method in a BMP entity bean?
The ejbPostCreate() method can be used for passing the reference of an
EJB object to other entity beans.
Collaborate
Lesson 2C / Slide 20 of 22
Collaborate
Challenge
1.
2.
3.
4.
5.
Stateful session beans save client’s conversational state in____________.
Persistent objects in the J2EE enterprise bean applications are represented
using _________beans.
Entity beans are of two types, _________ and __________.
The _________ method should have the same parameters as the
ejbCreate() method in a BMP entity bean.
The context object stores the information about the ___________ of an EJB.
Collaborate
Lesson 2C / Slide 21 of 22
Collaborate
Solutions to Challenge
1.
secondary storage
2.
3.
4.
entity
bean-managed persistence entity beans and container-managed persistence
entity beans
ejbPostCreate()
5.
environment
Collaborate
Lesson 2C / Slide 22 of 22