Many applications (procedures) require a specific condition to find entity Beans in the database. With the EJB-QL query language, entity Beans can define a Find method in the Home interface, query through entity beans. Add a Find method to do 2 steps: add an Find method in the home interface to join the EJB-QL query on the deployment of Beans. Resin-cmp uses EJB-QL query languages of EJB2.0 specification. EJB-QL is similar to SQL, and EJB-QL adds some restrictions and extensions in order to suit the operating entity bean, EJB-QL is compared to SQL. For example, EJB-QL expands SQL, directly supports association and method parameters.
In the case, the school's principal needs to know which course is in class and the teacher.
Database Schema Coursees.Sqlcreate Table Find_courses (Course_id Varchar (250) Not Null, Instructor Varchar (250),
Primary key (course_id)); LeJB-QL In the FindAll and FindbyStructor methods, LEJB-QL uses EJB-QL to select a teacher from the database. EJB-QL is similar to a certain restricted SQL, joining direct support for associated and method parameters. Findall method checks the database All courses, Schema Name Course from Abctract-Schena-AME. Field courseid is a CMP-Field name that does not have to match the table name and column name of SQL. If the table name and column name of SQL are changed in the deployment configuration, EJB-QL still refers to the same Abstract name. Select O from Course O
Findbyinstructor looks up courses with the method of parameter Instructor, because Instructor is a parameter, which must be specified in the query. EJB-QL uses the parameter method? N. n is the serial number of the parameter, starting from 1. SELECT o from course o where o.instructor =? 1
CLIENT Serverlet In the following example, we list three Find methods 1. Usually used FindByPriaryKey2. Returns the collection Findall3. FindbyStructor with an instructor parameter into a local object and its HOME interface complies with the role in the Home interface in Factory mode (factory mode in design mode) (Remote HOME's Find method always returns a set of remote interfaces or remote interfaces) If the application needs to return other values, including local beans, you need to call the EJBSELECT method. The FindAll method returns a collection containing all courses. The Client ends iterate over the course of the course.
FindAll Courses ...
Collection c = home.findall (); iterator it = c.ITerator ();
While (ore.hasnext ()) {course course = (course) iter.next ();
Out.println (course.getcourseid () "is taught by" course.getinstructor () "
");}
... results similar to the following: Potions is taught by Severus SnapeTransfiguration is taught by Minerva McGonagallDefense Against the Dark Arts is taught by findByInstructor method on patients Remus Lupin the parameters instructor returns a teacher taught courses, single-value lookup method expectations Returns one and there is only one result. If you don't find the result, Findbyinstructor throws an ObjectNotFoundException. If the return value is more than one, it will throw a FINDEREXCEPTION. Finding a course by ITS Instructor ...
Course course = home.Findbyinstructor (Teacher);
Out.println (course.getcourse () "is taught by" course.getinstructor () "
");
... Return results Similar: Potions is Taught by Severus Snape
Entity Bean class
The CourseHome interface defines the FindAll and the FindByinstructor method, and there is a FindByPrimaryKey method, and this method does not implement the class in the class that implements the interface, and the resin-cmp will automatically generate its code.
Coursehome.java package example.cmp.find;
Import java.util. *; import javax.ejb. *;
Public Interface CourseHome Extends EjblocalHome {Course FindbyPrimaryKey (String Name) throws FinderException;
Collection FindAll () Throws FinderException;
Course Findbyinstructor (String Instructor) Throws FinderException;
Course.java
Package example.cmp.find;
Import javax.ejb. *;
Public interface course extends ejblocalobject {string getcourse ();
String getinstructor ();
CourseBean.javaPackage Example.cmp.find;
Abstract public class coursebean extends com.caucho.ejb.abstractentityBean {Abstract public string getCourse ();
Abstract public string getinstructor ();
Deployment DescriptorDeployment Descriptor new feature is the
WEB-INF / CMP-FIND.EJB
Tag MeaningQuery Contains The Query Information for a Find MethodQuery-Method The Method DescriptorMethod-Name The Method Nameejb-QL The Query for the Find Method