The features and functions of the JDO 2.0 query Language

Source: Internet
Author: User
Tags definition expression final numeric min new features reference

The improvement of query language is an important part of JDO2.0 specification, and this paper expounds some new functions provided by JDO2.0 from a higher level. As the JDO2.0 norm has not yet entered the open draft State, nothing has been finalized and everything is likely to be subject to change. However, JDO2.0 will soon be in the final stages, and the query feature mentioned here is the JDO2.0 Expert group (translator: David Jordan is an important member of the Expert Group) who spends the most time and is relatively the most stable. Therefore, I have reason to believe that the final specification and the description here will be basically consistent.

If your readers feel that this article omits some important features, it is recommended that you immediately go to the JDO Forum (http://www.jdocentral.com/forums/index.php?showforum=10) to present and discuss it. Here we need to thank JDO2.0 Spec leader Craig Russell for empowering me to expose the new features of these JDO2.0 query languages.

Query results

We start with the most in-depth improvements. In JDO1.0, the query result is always a collection of instances of the class you specify. Consider the following UML class diagram, which expresses the relationships between classes A, B, C, and D four:

You can create a query for Class A, refer to Class B by contains (), and then refer to Class C with another layer of contains (), and finally use a "." The operator references to Class D. But the final returned collection will only contain object instances of Class A, and if you want to derive other classes from the results, you must obtain the related other class objects individually by reference to Class A. If your query criteria contain a class B, C, or D constraint, then you must repeat the constraints in the Java code when you refer to the other class objects through a Class A object in the result set, which means you have to repeat the restrictions in Java and JDOQL. Furthermore, you may only be concerned with Class D objects that satisfy the query criteria, and do not want intermediate B and C objects to be created by the JDO low-level to save memory or related resources.

In JDO2.0, you are no longer bound by these limitations. You can return:

One or more fields of a data class (persistentcapable)

Class objects other than the candidate class

Statistical data

This means that you can return a, B, C, D objects, or some of their fields, or the mixed results of both. You can also calculate statistical results such as Min or Max. Basically, you can return whatever results you want.

When you create a query, you can specify a "result specification" to specify what content to return. It is a comma-delimited "result Expression" that contains one or more. The resulting expression can be:

This keyword, which represents an object instance that returns a candidate class. This is the same field as JDO1.0, which indicates the value of a candidate class or a field of a reference class, such as a address.street.name field expression, that represents a result variable derived from several arithmetic operations of JDO predefined for multiple fields, representing an intermediate variable reference expression that appears in a query condition , that is, the passage in JDO1.0 "." operator, you can get any result you want by using the combination of the above expressions. "----statistical expressions

JDO2.0 supports the following statistical functions:

COUNT (expression), which can be an expression of this

sum (numeric field expression), "numeric field expression" can be the result of a numeric type obtained by the operation of a field or field

Min (numeric field expression)

Max (numeric field expression)

AVG (numeric field expression)

The query results are specified through the following APIs:

void Javax.jdo.Query.setResult (String result)

If you do not call this method, or if the parameter is null, the object instance of the candidate class (equivalent to "This" is set to "this") is returned, that is, the return result of the JDO1.0. If you specify only a unique result expression, the element type of the returned collection is the same as the type of the result. In addition, by default, if more than one result expression is specified, the returned collection element type will be object[].

You can distinct the beginning of the result definition string to ensure that the result is not duplicated. If the result definition string contains several expressions, then distinct can guarantee that there will be no duplicate data sets in the result set.

Each result expression can specify a name, and for a simple field, the system defaults to the name of the field as the name of the item in the result. For complex expressions, you can use the following syntax to specify a name:

Result_expression as Name

The use of a name allows the item in the result to be set and used as a property in the result class. You can specify a result class to return the query results. If the query result is a single value, the result class can be any of the classes supported by JDO (Integer, Double, String, BigInteger, BigDecimal, Java.util.Date, Java.sql.Date, Java.sql.Time, Java.sql.Timestamp). The method of setting the result class in query is:

void Setresultclass (Class resultclass)

If the query result contains more than one result expression, you can define a result class that preserves the data in the result, and the class must have a constructor without parameters. In addition, each result expression must correspond to a property in this class, whether it is a public field or a public setxxx () method, and the direct or bean-style property name is consistent with the names of the resulting expressions in the query results.

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.