Chapter 11 Inquiry
You can get a query instance from the PersistenceManager. The query can filter a batch of candidate objects that meet certain standards. This filtering process may be made in the database, or execute in memory. JDO does not express the query mechanism. Many JDO implementation will Situation mixing uses memory execution mode and database execution method. Now research query interface. You will find yourself confused with things that will be discussed. This is expected. We will explain some specific examples.
11.1 Required query elements
Each query has three essential elements: 1) Candidate class. The filtering of queries is performed for an instance of the candidate class or subclass. Candidate class is specified by the setClass method. 2) A batch of candidates can be a collection of objects Form or extension. These two cases can call setcandidate to specify candidates. If an extension is given, then you don't have to specify a candidate class for the query. The query will automatically inherit the extension candidate class. (?? Translation is not good) 3) Screening conditions. This is written with JDO query language JDOQL and specified by the setfilter method. If the filter criteria is not specified, the query returns all objects of the candidate class.
11.2 Optional Query Elements The following is an optional element of the JDO query 1) imports. Import is specified by the DeclareImports method. According to the standard Java import syntax, import is a single ";" divided string. Declaration parameters and variables When you use the import you don't have to enter a full name of the class. 2) Parameter declaration. The parameter is a placeholder in the string. It allows you to write a query, then execute multiple times. Each time you provide new value parameters .DeclareParameters method specifies that the parameter syntax of this method is consistent with the parameter declaration syntax of the JVA method. 3) Variable declaration. Variables are used to detect some items in the collection. Some conditions are matched. Syntax and standard Java variables. Declaring syntax consistent. 4) Sort. Sometimes you want the results of the query to return in the specified order. For example, you want to sell the sedale of the sedan, setordering methods allow you to add queries. The string parameter is Sort by "," separated list. Each declaration is added to the field name "Ascending" or "Descending" consists. The result will be sorted in the first (leftmost), then sorted, then there are two records In line with expressions, the second sort will be executed. Push it in this class.
11.3 JDOQL
JDOQL is a query language that is independent of Java Boolean expressions. Syntax Similar to Java Standard Syntax, only the following exceptions: the equalization and sorting of basic types and basic type packages are effective
The equalization and sorting comparison between the dates is that effective equivalent comparisons are usually used using the "==" operator, and the equals method is not supported.
Not supported. (=, =, * =, Etc) and and - equal assignment operators
Only the following methods are supported: collect.contains, collect.isempty, string.startswith and string.endswith
Flip the field of the NULL value will throw NullPointersException instead of the value of the child expression is false.
Support the following words: characters, integer, floating point, Boolean, string, and null (based on Valid Valid Equarisons equivalent comparison TRALERSE ??)
We will give some examples of query interfaces and JDOQL. This example uses the following energy-lasting classes.
Find all the Magazine objects greater than 10 values. Note We can use the persistence field of the candidate class in the filter string. JDOQL is only based on the object model, not the specific embodiment of the class provided by the database.
Find the Title field with the Magazine class starting with "The", sorted in ascending order of Price.
Find out that sales of 0.01 times larger than publishers taxes are found. Safe as sequencing of publishers and descending order of Publisher's Name fields. Note "." NS, remove the Revenue and Name fields from Publisher.
Find all the Price less than 10 publishers is Random House or Addison Wesley Magazine to find out the Magazine: Printing from the company. Price greater than given the number. Note that the company name and number are inquiring the string as a placeholder. Do not provide values before the query is not executed. And we use "import" to import org.mag.pub packages, then we can declare the type of Company's parameters. We can also use the import method, specify the full name of the class to declare parameter.
Find such a Magazine: Cover article has a "THE RAL Story" subtitle or cover article no subtitle
Find such a Magazine: The title of the article is "Fourier Transforms", pay attention to the usage of the variable. Any article representing Magezine's article collection. Find this Magazine: Its Article Title is a batch of possible Title One. Here we use set parameters to save all possible values, this is the usage of IN in SQL.
11.4 Executive Inquiry
As shown above, the query interface has many ways to execute queries. The parameters used by each query method are different. From 0 to three. You can use the Executewitharray and ExecuteThMap methods. Details See Query interface Javadoc document All execution methods Returns the object type. But in fact they return a collection. Jdo uses Object instead of a collection, enabling the vendor to return a specific type in some cases and allow future expansion.
The results of the query will take up the database resources. So it is best to turn it off when the query is no longer used. You can turn off individual queries or close all open queries.
11.5 query compilation
The query object can be compiled with Compile method. This means optimizing the planned query. During compilation, all query elements are valid, any inconsistency will throw jdouseRexception. Query instance can also be serialized. After serialization, query It cannot be performed. However, it also retains its candidate class, filter strings, import, parameters, variables, and dispatched queries can be used as a template for other queries as the same configuration. This is NewQuery of PersistenceManager. Object Template method to complete