Title: 8' ContainerManaged Persistent Entity Beans
18. Container-Managed Persistent Entity Beans
- CMP
- Container-Managed Persistent
- in this chapter EJB 2.0 CMP2.0 (CMP1.0)
-
- The EJB container performs storage operations
for us, we dont write any JDBC or SQL/J codes. -
2Features of CMP Entity Beans
- The difference between BMP and CMP
- CMP Entity Beans Are Subclassed
- database-independent
- clear separation between an entity bean and its
persistent representation (logic methods and the
JDBC) - superclass our job
- subclass container generates them by
subclassing our entity bean class - //The generated subclass inherits from our
entiity bean class
3Features of CMP Entity Beans
- CMP Entity Beans Have No Declared Fields
- The persistent fields are kept in the subclass.
//CMP Subclass, generated by Container Vendors
tool public class AccountBeanSubClass extends
AccountBean public String accountID//PK
public String ownerName public double
balance ...methods...
4CMP Class Hierarchy
5Features of CMP Entity Beans
- CMP Get/Set Methods Are Defined in the Subclass
- The subclass implements the get/set methods, the
super-class defines the declaration (abstract) of
the get/set methods.
public abstract class CartBean implements
EntityBean ... public float
getTotal() return this.getSubtotal()thi
s.getTaxes() ... //These methods,
similar with getTotal(), can not be automatically
generated by the container, we need to write
these methods in the super-class by ourselves.
6Features of Entity Beans
- CMP Entity Beans Have an Abstract Persistent
Schema - When deploy the J2EE application, we will set a
abstract persistent name for each CMP Bean. - The container inspects the deployment
descriptors to figure out what to generate. - (See also ejb-jar.xml in .ear package)
7Features of Entity Beans
- CMP Entity Beans Have a Query Language
- EJB-QL EJB Query Language, object-oriented SQL
syntax - CMP 2.0, EJB 2.0
- finder (used by external entity bean clients)
- select (used internally by the entity bean itself
) - SELECT, FROM, WHERE clause
-
8EJB-QL Terminology
- Abstract schema The part of an entity bean's
deployment descriptor that defines the bean's
persistent fields and relationships. - Abstract schema name A logical name that is
referenced in EJB QL queries. You specify an
abstract schema name for each entity bean with
container-managed persistence. - Abstract schema type All EJB QL expressions
evaluate to a type. If the expression is an
abstract schema name, by default its type is the
local interface of the entity bean for which the
abstract schema name is defined. - Backus-Naur Form (BNF) A notation that describes
the syntax of high-level languages. The syntax
diagrams in this chapter are in BNF notation. - Navigation The traversal of relationships in an
EJB QL expression. The navigation operator is a
period. - Path expression An expression that navigates to
a related entity bean. - Persistent field A virtual field of an entity
bean with container-managed persistence it is
stored in a database. - Relationship field A virtual field of an entity
bean with container-managed persistence it
identifies a related entity bean.
9EJB QL
- EJB-QL is a standard and portable language.
- EJB-QL SQL
- EJB-QL OQL
- EJB-QL Key Words
- AND AS BETWEEN DISTINCT EMPTY FALSE FROM IN IS
LIKE MEMBER NOT NULL OBJECT OF OR SELECT TRUE
UNKNOW WHERE -
- EJB-QL Data Type
- String in EJB-QL quoted by single quotes, for
example, OK - Integer long
- Float float, double
- Boolean TRUE, FALSE
Translate into
10EJB QL
- EJB-QL Parameters
- Only used in WHERE clause
- ?1, ?2, Indexed from 1 instead of 0
- Parameters data type should be identical with
the parameters in finder or select methods. - EJB-QL Comparison Operator
- ! in Java
- ltgt in EJB-QL
- EJB-QL Other Symbols
- zero or more characters
- _ a single character
11EJB QL
- Abstract Schema Name
- EJB-QL Functions
- CONCAT(String, String) return String
- SUBSTRING(String, start, length) return String
- LOCATE(String, String, start) return int
- LENGTH(String) return int
- ABS(number) return int, float or double
- SQRT(double) return double
12EJB QL
Abstract Schema Name
- A Simple Example
-
- SELECT OBJECT(p) FROM Product AS p WHERE
p.productID?1 - SELECT OBJECT(p) FROM Product AS p WHERE
p.productPrice BETWEEN 100 AND 300 - SELECT OBJECT(p) FROM Product AS p WHERE
p.productPrice gt ?1 - SELECT OBJECT(p) FROM Product AS p WHERE
p.productName LIKE Mobile - SELECT DISTINCT OBJECT(p) FROM Product AS p
WHERE p.productName ?1 AND p.productPrice gt ?2
13EJB-QL Relationship
- Examples
- SELECT DISTINCT OBJECT(p) FROM Player p, IN
(p.teams) AS t WHERE t.city ?1 - Data retrieved
- The players whose teams belong to the specified
city. - Finder method
- findByCity(String city)
- Description
- p represents Player Entity Bean
- t represents Teams Entity Bean
- p.teams navigates from Player bean to its
related Teams bean, navigation operator - IN(p.teams) signifies that teams is a
collection of related beans - t.city delimiter, city is a persistent field
- p.teams.city ERROR, because the teams is a
collection
14EJB QL
- EJB-QL Effective Range
- EJB-QL is useful for both home interfaces and
local home interfaces. A single EJB-QL definition
will inform the container about how to implement
the SQL code for both a home object and local
home object that have identically named finder
methods. - SELECT Method
- In Bean Class
- public abstract Collection ejbSelectAllProducts()
- Whether should the ejbSelect method return EJB
local objects or EJB objects - public abstract String ejbSelectProductName()
- ejbSelect methods can return container managed
fields, not does finder methods
15EJB-QL Restriction
- Comments are not allowed.
- Date and time values are in milliseconds and use
a Java long. A date or time literal should be an
integer literal. To generate a millisecond value,
you may use the java.util.Calendar class. - Currently, container-managed persistence does not
support inheritance. For this reason, two entity
beans of different types cannot be compared
16Features of Entity Beans
- CMP Entity Beans Can Have ejbSelect Methods
- A ejbSelect method is like a finder method, but
is not directly exposed to the client. ejbSelect
methods is used internally within an entity bean.
//ejbSelect() public abstract Collection
ejbSelectAllAccountBalances() throws
FinderException //ejbHome() public double
ejbHomeGetTotalBankValue() throws Exception
//Get a collection of bank account
balances Collection c this.ejbSelectAllAccountBa
lances() //Loop through collection and return
sum
17Java Sources
- J2SDKEE developing
- (See also demo 1, Chapter8/cmp/readme.txt)
- (See also demo 2, Chapter8/cmpNoPK/readme.txt
, - no primary key class)
- WSAD
- (See also demo 3, Chapter8/WASD/.avi and
.ear)