J2EE study note 7: EJB Query Language)

Source: Internet
Author: User

The Entity Bean provides an object-oriented abstraction for the underlying database. It can also create some business methods to operate on the data contained in the Entity Bean. To improve the convenience and efficiency of data operations, J2EE provides a type of SQL language EJB Query Language, abbreviated as EJB-QL, which is like the hql (Hibernate query language) we use in hibernate) same. By using the EJB-QL, We can insert a query similar to the SQL syntax in the method of the Entity Bean, the result of the EJB-QL query is usually the reference of the Entity Bean, you can directly operate on it, in this way, the advantages of SQL and object-oriented technology can be used at the same time.

There are two ways to use EJB-QL in an Entity Bean: using the EJB-QL find method in the home interface of the Entity Bean and using the EJB-QL select method in the bean class of the Entity Bean, the select method can only be called by other methods in the bean class. The following describes how to use the find method in the home interface of the object bean.

Suppose there is an EJB application that contains an Entity Bean named planet. Now we want to declare such a find method in its home interface: its function is to return all planets with a specified number of satellites. This method may be as follows: public collection findbynummoons (INT moons) throws finderexception; its corresponding EJB-QL may be something like this:

Select object (P)

From Planet P

Where p. nummoons =? 1

Analyze the above SQL language ,? 1 is used to pass parameters, just as we use question marks to specify parameters when using preparedstatement in Java. Select object indicates that the returned result of this query is a reference of an entity bean or a collection containing multiple Entity Bean references. (P) indicates the type of the returned object bean, which is linked by the following Planet P, it indicates that the name of the abstract schema corresponding to the returned object bean is planet. Of course, the find method can also return only a reference of a separate entity bean, rather than a set.

In addition, there are four main differences between the find and select methods:

● The find method is declared in the home interface of the object bean, while the select method is declared in the bean class of the object bean.

● The find method is visible to other beans, while the select method can only be accessed by methods in the same object bean.

● The find method can only return the bean interface reference of the Entity Bean or a set of these references. The select method can also return the values of the CMP domain and the set of these values.

● The find method is in the findxxxx () format, while the select method is in the ejbselectxxxx () format.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.