ejbSelect for more EJB-QL queries
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

User's Guide
Reference
Tutorials
Scrapbook

Basic CMP
Find with EJB-QL
Creating and Removing
1-n Relationship
ejbSelect EJB-QL
1-1 Relationship
n-m Relationship
Map/Compound PK
1-n Relationship
Tutorials
1-1 Relationship

Find this tutorial in: /usr/local/resin/webapps/resin-doc/cmp/tutorial/cmp-select
Try the Tutorial

  1. Database Schema
  2. ejbSelectBoys

ejbSelect provides a more flexible method to query the database than find methods. Find methods belong to a home interface and only return the corresponding local object. A findFoobar method in StudentHome can only return a Student or a collection of Students. ejbSelect methods can return any type in the database or a collection of database objects. They are restricted to the entity bean implementation class; you'll need to write a separate business method to expose the results of the query.

Like find methods, ejbSelect methods use the deployment descriptor to define the EJB-QL query. Resin-CMP will generate the appropriate SQL for the select.

Database Schema

select.sql
CREATE TABLE select_student (
  name VARCHAR(250) NOT NULL,
  house VARCHAR(250) NOT NULL,
  gender VARCHAR(6) NOT NULL,

  PRIMARY KEY(name)
);

CREATE TABLE select_house (
  name VARCHAR(250) NOT NULL,

  PRIMARY KEY(name)
);

ejbSelectBoys

The ejbSelect method for the example is in the HouseBean class. There are no real limitations on which bean implemenation has the ejbSelect method. All the entity beans in the database are accessible. However, ejbSelect methods are not visible in either the home or the local interfaces. If the bean needs to make the results visible, you'll need to create a business method to collect and return the selected results.

Unlike the find methods, ejbSelect needs an abstract method in the bean implementation. Finds declare the find method in the home interface; selects declare the ejbSelect method in the bean implementation.

public abstract Collection ejbSelectAllBoys(House house)
  throws FinderException;

The select query is defined in the deployment descriptor like the find query. The following is the query for the example.

Select boys query
SELECT student.name
FROM select_house house, IN(house.studentList) student
WHERE student.gender='Boy' AND house=?1
ORDER BY student.name

The required SELECT clause may return any cmp-field or cmr-field or any collection of either. In this case we'll return a collection of strings. This return value would be illegal in a find method because a find method could only return Student (and HouseHome couldn't return the Student interface from a find method.)

The required FROM clause defines the abstract tables, traversing relations as necessary. In this case, we define the variable student to range over all the students in the house. All collection-valued relations will use IN expressions to define a query variables.

The optional WHERE clause can use normal expressions like '=' or boolean expressions. WHERE can use ejbSelect arguments with the '?1' expression.

The optional ORDER BY clause is a Resin-CMP extension to EJB-QL. It orders the select so the boys will be listed alphabetically. Because the ORDER BY clause is so useful, a future version of the EJB 2.0 spec will certainly include it.

Try the Tutorial


1-n Relationship
Tutorials
1-1 Relationship
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.