40
Advanced Features of CMP Entity Beans Lesson 3B / Slide 1 of 40 J2EE Server Components Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR) Implement CMR in CMP entity beans Implement EJB Query Language (EJB QL) in CMP entity applications

Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

  • Upload
    baby

  • View
    15

  • Download
    0

Embed Size (px)

DESCRIPTION

Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR) Implement CMR in CMP entity beans Implement EJB Query Language (EJB QL) in CMP entity applications. Pre-assessment Questions Consider the following statements: - PowerPoint PPT Presentation

Citation preview

Page 1: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 1 of 40J2EE Server Components

Objectives

In this lesson, you will learn to:

• Describe Container-Managed Relationships (CMR)

• Implement CMR in CMP entity beans

• Implement EJB Query Language (EJB QL) in CMP entity applications

Page 2: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 2 of 40J2EE Server Components

Pre-assessment Questions1. Consider the following statements:

Statement A: In case of CMP beans, you can make minor modifications and reuse the bean with other database technologies.Statement B: In case of BMP entity bean, you need to hard code the logic to query and define a database and handle persistence. Identify the correct option.a. Statement A is true and Statement B is false b. Statement A is false and Statement B is true c. Both, statements, A and B, are true d. Both, statements, A and B, are false

Page 3: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 3 of 40J2EE Server Components

Pre-assessment Questions (Contd.)2. Which method is called by EJB container for associating a CMP entity bean

with an entity context object? a. ejbSelect()

b. setEntityContext()

c. ejbLoad() d. ejbCreate()

3. Which feature of the CMP entity bean specifies that the deployment tool generates complex data access and management code automatically? a. Maintenance b. Portability c. Implementation d. Flexibility

Page 4: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 4 of 40J2EE Server Components

Pre-assessment Questions (Contd.)4. Which tag of the deployment descriptor specifies the type of the primary key

field? a. <primkey-field> b. <fieldname> c. <primkey-class> d. <cmp-field>

Page 5: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 5 of 40J2EE Server Components

Pre-assessment Questions (Contd.)5. Consider the following statements:

Statement A: In CMP entity bean, you need to provide the logic to implement referential integrity.Statement B: In BMP entity bean, EJB container provides referential integrity. Identify the correct option.a. Statement A is true and Statement B is false b. Statement A is false and Statement B is true c. Both, statements, A and B, are true d. Both, statements, A and B, are false

Page 6: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 6 of 40J2EE Server Components

Solutions to Pre-assessment Questions 1. c. Both, statements, A and B, are true 2. b. setEntityContext() 3. a. Maintenance 4. c. <primkey-class>5. d. Both, statements, A and B, are false

Page 7: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 7 of 40J2EE Server Components

Container-Managed Relationships (CMR)

• Container-Managed Relationship:

• Refers to the link between the attributes of two or more entity beans.

• Has the following features:

• Directionality

• Cardinality

Page 8: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 8 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Directionality of Relationships

• Directionality stipulates the direction in which you can navigate a relationship.

• In a relationship involving two entity beans, A and B, if the direction of the relationship is from A to B then A is known as the source or parent entity bean and B is known as the destination or child entity bean.

• Relationships can be:

• Unidirectional: Data only navigates in one direction.

• Bi-directional: Data can navigate in both directions.

• You can specify the directionality of a relationship by declaring the accessor methods for the CMR fields of the relationship in the code of entity beans.

Page 9: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 9 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Cardinality of Relationships

• In a relationship, only two entity beans can participate at a time. One entity’s value or values can be related to one or more values in another entity. This characteristic of a relationship is known as cardinality.

• Various types of cardinalities in relationships are:

• One-to-one

• One-to-many

• Many-to-one

• Many-to-many

Page 10: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 10 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• One-to-One Relationship

• Exists between any two-entity beans, A and B, in which a single value in A is linked to a single value in B.

• Expressed as 1:1.

Page 11: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 11 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• One-to-Many Relationship

• Exists between any two entity beans, A and B, where a value of the entity bean A is related to multiple values of the entity bean B.

• Expressed as 1:* or 1:N.

Page 12: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 12 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Many-to-One Relationship

• Exists between any two entity beans, A and B, where multiple values of entity bean, A are related to a single value of entity bean B.

• Expressed as *:1 or N:1.

Page 13: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 13 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Many-to-Many Relationship

• Exists between any two entity beans, A and B, where multiple values of entity bean A are related to multiple values of entity bean B.

• Expressed as *:* or N:N.

• The two ways of implementing many-to-many relationships are:

A fake many-to-many relationship: Is implemented between two tables by creating an association table in a database. The association table consists of the foreign keys to the other two tables.

In true many-to-many relationships: Indicates that two entity beans are directly related to each other, without the aid of additional tables or beans.

Page 14: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 14 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Multiplicity of an Entity in a Relationship

• Defines whether a bean participating in a relationship can be related to one or many beans.

• multiplicity is a feature of an individual entity in a relationship.

• Multiplicity of the entity bean A, with respect to this relationship is, one.

• Multiplicity of the entity bean B with respect to this relationship is many.

Page 15: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 15 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Other Types of Relationships

• In addition to the directionality and cardinality-based relationships, the other types of relationships are:

• Recursive

• Circular

• Chain

Page 16: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 16 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Recursive Relationship

• A relationship is called recursive if an entity bean is associated with itself.

• A recursive relationship is also known as a self-relation.

• Manager–to-employee self-relation:

Page 17: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 17 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Circular Relationship:

• Exists between three or more beans.

• Circular relationship between the employee, department, and project beans:

Page 18: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 18 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Chain Relationship:

• Exists between the beans, A, B, and C, where A is related to B and B is, in turn, related to C.

• Chain relationship between the department, employee, and workstation beans:

Page 19: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 19 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• Deployment Descriptor Elements for Specifying CMR

• In a deployment descriptor, a relationship between two entity beans is described in two parts, for each entity bean participating in the relationship.

• Tags used in the deployment descriptor for describing information about CMRs are: • <relationships>…</relationships>: Specifies the beginning and

end of information about CMRs in a deployment descriptor.• <ejb-relation>…</ejb-relation>: Specifies the beginning and the

end of a relation.• <ejb-relation-name>…</ejb-relation-name>: Specifies an alias or

name of a relationship.

Page 20: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 20 of 40J2EE Server Components

Container-Managed Relationships (Contd.)

• <multiplicity>…</multiplicity>: Defines the multiplicity of an entity bean participating in a relationship. This tag accepts the values: One or Many.

• <relationship-role-source>…</relationship-role-source>: Describes an entity bean participating in a relationship.

• <ejb-relation-name>…</ejb-relation-name>: Specifies an alias or name of a relationship.

• <cmr-field-name>…<cmr-field-name>: Defines the name of a CMR field.

Page 21: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 21 of 40J2EE Server Components

Demonstration-Implementing CMR in CMP Entity Beans

• Problem Statement

• David and his team are building an online application for a bank where each customer can have multiple accounts. They need to create appropriate objects and represent them using CMP entity beans.

Page 22: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 22 of 40J2EE Server Components

Demonstration-Implementing CMR in CMP Entity Beans (Contd.)

• Solution

• To solve the preceding problem, perform the following tasks: 1. Create the Account CMP entity bean home interface. 2. Create the Account CMP entity bean local interface. 3. Create the Account CMP entity bean class. 4. Create the Customer CMP entity bean home interface. 5. Create the Customer CMP entity bean local interface. 6. Create the Customer CMP entity bean class.

Page 23: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 23 of 40J2EE Server Components

Demonstration-Implementing CMR in CMP Entity Beans (Contd.)• Solution (Contd.)

7. Create the Web client. 8. Package the CMP entity bean, account. 9. Package the CMP entity bean, customer. 10.Setting CMR between the customer and account CMP Entity

Beans. 11.Package the Web client. 12.Deploy the application. 13.Test the application.

Page 24: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 24 of 40J2EE Server Components

Enterprise JavaBeans Query Language (EJB QL)

• EJB QL:

• Is used implement queries in entity beans.

• Supports traversing relationships between entities and working with collection-based CMR fields.

• Syntax of EJB QL

• EJB QL is similar to SQL in terms of usage and syntax.

• An EJB QL query is framed using statements that are similar to sentences in English.

• EJB QL consists of the following clauses:

• SELECT

• FROM

• WHERE [Optional]

• ORDER BY [Optional]

Page 25: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 25 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• The SELECT Clause:

• Defines the result, which can be an object or a value that the query returns.

• Is defined using the following syntax:

SELECT [DISTINCT]

{

Single-valued-path-expression|

OBJECT (identification-variable)

}

Page 26: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 26 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• The FROM Clause:

• Defines which entity beans are to be used in the query.

• Is defined using the following syntax:

FROM abstract_schema [AS] identification_variable

• The WHERE Clause:

• Has a conditional expression and the values that satisfy the condition in the expression are returned.

• Is defined using the following syntax:

WHERE conditional-expression

Page 27: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 27 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• The ORDER BY clause:

• Sorts the objects or values returned by a query into a specific order.

• Is defined using the following syntax:

ORDER BY field_item_1 [ASC | DESC field_item_2 ASC | DESC .. field_item_n ASC | DESC]

Page 28: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 28 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• The BNF Symbols

• The Backus Naur Form (BNF) is a meta language used for denoting the syntax of structured languages, such as DDLs, DMLs, and DQLs.

• The BNF symbols defined in EJB QL are:

• ::=: Defines the element that is on the left of the symbol. The definition of the element is provided on the right of the symbol. Syntax to implement the ::= symbol is:

EJB-QL ::= Select-clause from-clause[where-clause]

• *: Specifies that the construct preceding it can be present in the definition for zero or more times. Syntax to implement the * symbol in the IN expression is: in-expression ::=

single-valued-path_expression

[NOT] IN (string-literal [, string-literal]*)

Page 29: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 29 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• {..}: Groups multiple constructs, together. Syntax to implement the {..} symbol is: Select-clause::= SELECT [DISTINCT]

{Single-valued-path-expression | OBJECT(identification_variable) }

• | : Specifies the exclusive OR operation between two constructs. For example, the syntax to implement the | symbol is:

comparison-operator ::=

= | > | >= | < | <= | <>

• BOLDFACE: Is a keyword and it is not case-sensitive.

• Whitespace: Represents space, line feed, and horizontal tab.

Page 30: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 30 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• Literals in EJB QL

• A literal is a textual representation of a specific value.

• EJB QL supports the following types of literals:

• String: In EJB QL, enclosing alphanumeric character within single quotation marks is used for representing a string literal.

• Numeric: Represent hard coded numbers. EJB QL has two types of numeric literals: Exact and Approximate.

• Boolean: Can be TRUE or FALSE and it is not case-sensitive.

Page 31: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 31 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• Built-in Functions in EJB-QL

• The built-in string functions in EJB QL are: • CONCAT(String1, String2)

• SUBSTRING(String, start, length)

• LOCATE(String1, String2 [, start])

• LENGTH(String)

• The built-in arithmetic functions in EJB QL are: • ABS(number)

• SQRT(double)

Page 32: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 32 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• Precedence of Operators in EJB QL

• In the operator precedence list, an operator appearing above another operator has a higher precedence.

• The operators according to their decreasing precedence are:

• Navigation operator

• Arithmetic operator

• Comparison operators

• Logical operators

Page 33: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 33 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• Implementing EJB QL Queries

• EJB QL statements are defined in the deployment descriptor of a CMP entity bean.

• Tags used for implementing EJB QL statements in the deployment descriptor are: :

• <query>…</query>

• <method-name>…</method-name>

• <method-params> …</method-params>

• <method-param>…</method-param>

• <ejb-ql>…</ejb-ql>

Page 34: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 34 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• Path Expressions

• Define the navigation paths through the relations between the abstract schemas.

• Can appear in the SELECT, WHERE, and FROM clauses of an EJB QL query.

• Are of following types:

• The BETWEEN Expression

• The IN Expression

• The LIKE Expression

• The NULL Expressions

• The EMPTY Expression

Page 35: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 35 of 40J2EE Server Components

Enterprise JavaBeans Query Language (Contd.)

• EJB QL Restrictions

• The restrictions in EJB QL are:

• You cannot write comments in EJB QL.

• EJB QL does not have any support for comparing entity objects of different types as the CMP entity beans do not allow inheritance.

• In EJB QL, the value of date and time is in milliseconds and need to use the Java long millisecond value. You can generate a millisecond value by using the class, java.util.Calendar.

Page 36: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 36 of 40J2EE Server Components

Demonstration-Implementing EJB QL in a CMP Entity Bean Application

• Problem Statement

• In the online banking application, registered customers can view account details using their account ids. Each customer can have multiple accounts in the bank. David needs to use EJB-QL to enable customers to view details for a specific account.

Page 37: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 37 of 40J2EE Server Components

Demonstration-Implementing EJB QL in a CMP Entity Bean Application

(Contd.)• Solution

• To solve the problem, perform the following tasks :1. Create the CMP entity bean home interface.2. Create the CMP entity bean remote interface.3. Create the CMP entity bean class.4. Create the Web Client. 5. Package the CMP entity bean.6. Package the Web Client.7. Deploy the application. 8. Test the application.

Page 38: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 38 of 40J2EE Server Components

SummaryIn this lesson, you learned:

• Container-Managed Relationship (CMR) refers to the link between the attributes of two or more entity beans. In J2EE, EJB container handles the relationships among entity beans.

• Relationships can be either bi-directional or unidirectional. Directionality of a relationship stipulates the direction in which you can navigate a relationship.

• In unidirectional relationships, also known as one-way relationships, data navigates in one direction only. In bi-directional relationships, also known as two-way relationships, data can navigate in both directions.

•   One entity's value or values can be related to one or more values in another entity. This characteristic of a relationship is known as cardinality. Various types of cardinalities in relationships are: one-to-one, one-to-many, many-to-one, many-to-many.

Page 39: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 39 of 40J2EE Server Components

Summary(Contd.)• One-to-one relationships are rare in relational databases, as both the

entities exist in the same table. One-to-one relationships are used mainly in object databases.

• There are two ways of implementing many-to-many relationships, as fake relationships or as true relationships. A fake many-to-many relationship is implemented between two tables by creating an association table in a database. The association table consists of the foreign keys to the other two tables.

• The term, multiplicity, defines whether a bean participating in a relationship can be related to one or many beans.

• In a deployment descriptor, a relationship between two entity beans is described in two parts, for each entity bean participating in the relationship. You need to specify all the information regarding CMRs in the form of tags in the deployment descriptor.

Page 40: Objectives In this lesson, you will learn to: Describe Container-Managed Relationships (CMR)

Advanced Features of CMP Entity Beans

Lesson 3B / Slide 40 of 40J2EE Server Components

Summary(Contd.)• EJB 2.0 specification provides EJB QL to implement queries in entity

beans. EJB QL is similar to SQL in terms of usage and syntax. An EJB QL query consists of clauses: SELECT, FROM, WHERE, and ORDER BY. The WHERE and ORDER BY clauses are optional.

• EJB QL supports the String, Numeric and Boolean literals.

• There are some restrictions in EJB QL. You cannot write comments in EJB QL. EJB QL does provide support for comparing entity objects of different types as the CMP entity beans do not allow inheritance.