JDO Query Introduction

zhaozj2021-02-16  48

Query introduction

You can get a query object through the PersistenceManager. Query object is used to query data from the persistent object. Below we will discuss each part of Query in detail.

1. Mustful query element

Public void setclass (class candidateclass);

Public void setcandidates (extent Candidates);

Public void setcandidates (Collection Candidates);

Public void setfilter (String filter);

Query has three must-have elements:

1) Candidate class: Candidate class or subclass for query, this candidate class is set by SetClass method.

2) Collection of candidate objects: Candidate classes can be placed in a collection or in an extent, you can set them through the setcandidates method.

3) Query strings: This is a JDOQL string, you can set it with setfilter.

2. Optional query element

Public void DeclareImports (String Imports) and Download (STRING IMPORTS);

Public void DeclareParameters (String Parameters);

Public void Declarevariables (String Variables);

Public void setORDERING (String ORDERING);

Query has 4 optional elements:

1) Import: It can be set by DeclareImports, which is a few Javaa Import syntax from the semicolon. This is similar to the @Page Import = "syntax in JSP. With this setting, you can do not have to process the outputs of the class name.

2) Parameter declaration: Used to set Query's query parameters, you can set it through DeclareParameters.

3) Variable declaration: The value used to set the query query parameter can be set by DeclareVariables.

4) Sort: Set the sorting method of the returned query result, the meaning is equivalent to the order by the SQL language, and can be set by setORDERING.

3. JDOQL

JDOQL is a query language that is unrelated to the specific database, which is very similar to the standard SQL syntax, but there are some differences:

1) Compare and sorting of the original type and the original type of package class.

2) Allow comparison and sorting of DATE objects.

3) The comparison operation uses "==" and does not support the Equals comparison operation.

4) Do not support =, =, * =, , etc.

5) Does not support the following methods:

Collection.Contains

Collection.isempty

String.StartSwith

String.endswith

6) If you get an null value will throw a nullpointException exception.

Now we will explain the use of JDOQL and Query, which will use the following persistence class

Package Org.mag;

Public Class Magazine

{

PRIVATE STRING TITLE;

PRIVATE DOUBLE PRICE;

Private int Copiesold;

Private Company Publisher;

Private articles; privately;

...

}

Public Class Article

{

PRIVATE STRING TITLE;

Private collection subtitles;

...

}

Package org.mag.pub;

Public Class Company

{

PRIVATE STRING NAME;

Private Double Revenue;

...

}

Example 1: The most basic query

Magazine in the query price is greater than 10 yuan. Note that we can also use persistent fields for persistence classes in query characters.

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "price> 10.0";

Query Query = PM.NewQuery (Mags, Filter);

Collection results = (color) query.execute ();

Example 2: Sorting and Method Call

The query is recorded as the beginning of the THE and arranges the result in ascending order.

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "Title.startSwith (/" THE / ")"

Query Query = PM.NewQuery (Mags, Filter);

Query.setOrdering ("price ascending"); // is arranged in ascending order

Collection results = (color) query.execute ();

Example 3: Accurate operation and relationship traversal

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "price * copiesold> Publisher.Revenue * .01";

Query Query = PM.NewQuery (Mags, Filter);

Query.setOrdering ("Publisher.Revenue Descending, Publisher.Name Ascending);

Collection results = (color) query.execute ();

The price income of the query magazine is more than 1%, and the price is arranged in descending order, and the name of the Press is arranged in ascending order.

Example 4: Priority and logical operation

The price of the query magazine is less than or equal to 10 yuan and the name of the publisher is Random House or Addison Wesley.

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "price <= 10.0"

"&& (publisher.name == /" random house / ""

"|| publisher.name == /" addison wesley / ")";

Query Query = PM.NewQuery (Mags, Filter);

Collection results = (color) query.execute ();

Example 5: Import and parameters

By a given company or price query belongs to this publishing house and the price is more than 10 magazines. DeclareImports imports the Company Class we need, and you have to import a COMPANY class by setting the string to "import org.mag.pub.company".

Company mycompany = ...;

Double myprice = ...;

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "Publisher == Pub && Price> amnt";

Query Query = PM.NewQuery (Mags, Filter);

Query.DeclareImports ("Import Org.mag.pub. *;");

Query.DeclareParaseters ("Company Pub, Double Amnt");

Collection results = (collection) query.execute (mycompany, myprice);

Example 6: Collection

The title of the query magazine contains "The real story" or there is no title.

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "CoverArticle.subtitles.contains (/" the real story / ")"

"|| CoverArticle.subtitles.isempty ()";

Query Query = PM.NewQuery (Mags, Filter);

Collection results = (color) query.execute ();

Example 7: Variable

Query the article titled "Fourier Transforms". Note The title here uses a variable Art to replace Article.

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "Articles.Contains (Art)"

"&& Art.title == /" Fourier Transforms / ";

Query Query = PM.NewQuery (Mags, Filter);

Query.Declarevariables ("Article ART");

Collection results = (color) query.execute ();

Example 8: Collection parameters

Query Articles The title meets the conditions, and the possible header is placed in a collection, which is a bit similar to the in operation in SQL.

Collection Titles = ...; // Possible Title Collection

Extent Mags = PM.Getextent (Magazine.class, False);

String filter = "Articles.Contains (Art)"

"&& titles.contains (art.title)";

Query Query = PM.NewQuery (Mags, Filter);

Query.DeclareParaseters ("Collection Titles");

Query.Declarevariables ("Article Art"); Collection Results = (Collection) query.execute ();

4. Execute query

Public Object Execute ();

Public Object Execute (Object Param1);

Public Object Execute (Object Param1);

Public Object Execute (Object Param2, Object Param3);

Public Object ExecuteTHARRAY (Object [] params;

Public Object ExecuteTEWITHMAP (Map params);

All queries are performed by Execute, which returned to the result in a Collection collection, contains objects of the result set. You can get the objects in the collection by a collection iteration. Finally close Query through Close

Public void close (Object Result);

Public void closeall ();

Example:

PersistenceManager PM = ...

Query Query = PM.NewQuery (Employee.class, "Salary> 50000");

Collection Employees = (Collection) query.execute ();

Try

{

ITERATOR ITR = EMPLOYEES.ITERATOR (); itr.hasnext ();)

Processemployee (EMPLOYEE) ITR.NEXT ());

}

Finally

{

Query.close (EMPLOYEES);

}

5. Query editing

You can use the Compile to compile queries before query so you can optimize your query. If you have problems with the query statement, you will throw a JDOUSEREXCEPTION exception.

Public void compile ();

转载请注明原文地址:https://www.9cbs.com/read-25136.html

New Post(0)