400 likes | 477 Views
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:
E N D
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 J2EE Server Components
Pre-assessment Questions • 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. • Statement A is true and Statement B is false • Statement A is false and Statement B is true • Both, statements, A and B, are true • Both, statements, A and B, are false J2EE Server Components
Pre-assessment Questions (Contd.) • Which method is called by EJB container for associating a CMP entity bean with an entity context object? • ejbSelect() • setEntityContext() • ejbLoad() • ejbCreate() • Which feature of the CMP entity bean specifies that the deployment tool generates complex data access and management code automatically? • Maintenance • Portability • Implementation • Flexibility J2EE Server Components
Pre-assessment Questions (Contd.) • Which tag of the deployment descriptor specifies the type of the primary key field? • <primkey-field> • <fieldname> • <primkey-class> • <cmp-field> J2EE Server Components
Pre-assessment Questions (Contd.) • 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. • Statement A is true and Statement B is false • Statement A is false and Statement B is true • Both, statements, A and B, are true • Both, statements, A and B, are false J2EE Server Components
Solutions to Pre-assessment Questions • c. Both, statements, A and B, are true • b. setEntityContext() • a. Maintenance • c. <primkey-class> • d. Both, statements, A and B, are false J2EE 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 J2EE 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. J2EE 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 J2EE 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. J2EE 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. J2EE 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. J2EE 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. J2EE 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. J2EE 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 J2EE 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: J2EE Server Components
Container-Managed Relationships (Contd.) • Circular Relationship: • Exists between three or more beans. • Circular relationship between the employee, department, and project beans: J2EE 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: J2EE 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. J2EE 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. J2EE 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. J2EE Server Components
Demonstration-Implementing CMR in CMP Entity Beans (Contd.) • Solution • To solve the preceding problem, perform the following tasks: • Create the Account CMP entity bean home interface. • Create the Account CMP entity bean local interface. • Create the Account CMP entity bean class. • Create the Customer CMP entity bean home interface. • Create the Customer CMP entity bean local interface. • Create the Customer CMP entity bean class. J2EE Server Components
Demonstration-Implementing CMR in CMP Entity Beans (Contd.) • Solution (Contd.) • Create the Web client. • Package the CMP entity bean, account. • Package the CMP entity bean, customer. • Setting CMR between the customer and account CMP Entity Beans. • Package the Web client. • Deploy the application. • Test the application. J2EE 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] J2EE 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) } J2EE 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 J2EE 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] J2EE 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]*) J2EE 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. J2EE 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. J2EE 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) J2EE 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 J2EE 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> J2EE 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 J2EE 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. J2EE 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. J2EE Server Components
Demonstration-Implementing EJB QL in a CMP Entity Bean Application (Contd.) • Solution • To solve the problem, perform the following tasks : • Create the CMP entity bean home interface. • Create the CMP entity bean remote interface. • Create the CMP entity bean class. • Create the Web Client. • Package the CMP entity bean. • Package the Web Client. • Deploy the application. • Test the application. J2EE Server Components
Summary In 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. J2EE 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. J2EE 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. J2EE Server Components