July 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Advanced Container-Managed Persistence

  • August 6, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

This material is from the book JBuilder Developer's Guide , written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

© Copyright Sams Publishing. All rights reserved.


Using the EJB 1.1 specifications, container managed persistence beans were not very powerful. In most cases, bean developers went quickly beyond the capabilities of CMP and had to convert to use bean managed persistence. Many developers shied away from using entity beans altogether. The EJB 2.0 quickly addressed many of these issues. This now allows the bean developer to implement most persistence requirements using container managed persistence. The four features that facilitate this are the following:

The Enterprise JavaBeans Query Language

The Enterprise JavaBeans Query Language (EJB QL) specification defines a query language to be used within finder methods of an entity bean with container managed persistence. Rather than develop a completely new query language, the specification is based on a subset of SQL92. The EJB QL gives you the features to allow navigation over the relationships defined in an entity bean's abstract schema.

Your EJB QL queries will be contained within your deployment descriptor of the entity bean. The container will then translate these queries into the target language of the underlying data store. For example, EJB QL will be translated to true SQL for JDatastore. Using this translation allows entity beans using container managed persistence to be portable. In other words, the code is not tied to a specific type of data store or application server vendor.

EJB Query Restrictions

EJB QL has a few restrictions:

  • Commenting is not supported within EJB QL.

  • Date and time values are always represented in milliseconds and contained within a Java long.

  • Container managed persistence does not support inheritance. In other words, you cannot compare two entity beans of different types with each other.

Query Syntax

An EJB QL query has three components associated with it: SELECT, FROM, and WHERE. The SELECT and FROM clauses are the only required portions of EJB QL; the WHERE clause is optional. For example, SELECT OBJECT(e) FROM Employee e selects all the instances contained within the Employee entity.

The SELECT component defines the values or objects that will be returned by the created query. The return values can either be a local interface or a remote interface contained within an entity. The FROM component defines the scope of the query. It may declare one or more entities. Finally, the WHERE component contains a conditional expression that restricts the values retrieved by the query. As mentioned earlier, this portion of the query is optional; it is normally required to meet most entity finder requirements.

Examples Described

SELECT OBJECT(e) FROM Employee e

This method returns all the instances of employee contained within the entity. In this case, we refer to the Employee entity with an alias e to make the definition of the query shorter.

SELECT DISTINCT OBJECT(e) FROM Employee e WHERE e.empNo = ?1

The query returns an employee instance specified by an empNo. This query is typically used to find an instance by either a primary key or alternate key. The DISTINCT keyword can be used to remove duplicates from the query but this should not happen if you are using a primary key.

SELECT DISTINCT OBJECT(e) FROM Employee e, IN (e.salaryHistory)
as s WHERE s.Country = ?1

This query navigates to related beans using the defined relationship. The e variable relates to the Employee entity bean, and the s variable represents the related bean instances SalaryHistory. The IN keyword specifies that SalaryHistory is a collection of beans related to the Employee entity. The query then navigates the relationship to identify all the salary histories within a specific country.

SELECT DISTINCT OBJECT(e) FROM Employee e WHERE e.salary BETWEEN ?1 AND ?2

The employees whose salaries fall within a given range are returned. The BETWEEN keyword returns all salaries within the given range. This expression is useful when using range finder methods.

Expressions and Functions

Just like SQL92 specifications of which EJB QL specifications is a subset, EJB QL describes the functionality of the expressions and functions. The specification does not include a full list of functions like that of SQL92 but simply a subset that should fit most requirements (see Table 1).

Table 1 Expressions and Functions Contained Within EJB QL

Type

Description

Standard comparisons

Just like SQL, you have the operations =, >, >=, <, <=, and <>.

Arithmetic

+, -, *, /

Logical

NOT, AND, OR

Between

The BETWEEN expression determines whether a value falls within a range.

In

The IN expression determines whether a value is contained within a given set.

Like

The LIKE expression evaluates whether a string matches a pattern. For a single character wildcard, use _. To use a wildcard that represents zero or more characters, use %.

Null

The NULL comparison expression evaluates whether an attribute or set is null. This is not used to evaluate a null collection; we use the EMPTY expression to evaluate collections.


EJB QL also includes several functions, both arithmetic and string, listed in Table 2. Most EJB vendors, including Borland's Application Server, allow you to define and implement your own custom functions.

Table 2 Functional Expressions

Function Syntax

Description

CONCAT(String, String)

Concatenate two strings together and return into a third.

SUBSTRING(String, start, length)

Return a substring based on the start and length passed in.

LOCATE(String, String [, start])

Locate a string within another and return its location.

LENGTH(String)

Return the length of a string.

ABS(number)

Return the absolute version of the number.

SQRT(double)

Return the square root of a number.






Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel