JDO: Querying your data

Source: Internet
Author: User
Tags extend

JDO Learning Notes (i)

Check your data

There are two interfaces that are closely related to the JDO API and queries (their specific implementation is done by the jdo product):

Java. Jdo. Query

========================

An instance of this interface is created by another interface PersistenceManager , and PersistenceManager is a more important interface in JDO. All of our transactions are defined through this interface.

the PersistenceManager interface has a set of newquery methods to produce an instance of Query:

Public Query NewQuery ()

Public Query newquery ( java.lang.Class cls )

Public Query newquery ( java.lang.Class cls, java.util.Collection cln )

Public Query NewQuery (java.lang.Class cls,

java.util.Collection cln,

java.lang.String filter)

Public Query newquery ( java.lang.Class cls, java.lang.String filter )

Public Query newquery (Extent  cln )

Public Query newquery ( Extent cln, java.lang.String filter )

Public Query newquery ( java.lang.Object compiled )

Public Query newquery ( java.lang.String language, java.lang.Object query )

There is a query that returns results, a set of Execute methods in the query interface to return the results of the query:

Public Object Execute ()

Public Object Execute ( java.lang.Object p1 )

Public Object Execute ( java.lang.Object p1, java.lang.Object p2 )

Public Object Execute (java.lang.Object p1,

 java.lang.Object p2,

 java.lang.Object p3)

Usually I do not use the above method, especially in the comprehensive query, I prefer the following two methods to return the query results:

Public Object excuteWithArray(java.lang.Object[] parameters)

public Object excuteWithMap(java.util.Map parameters)

 

当然如果是非常简单条件的查询,上边四种方法可以搞定就不要用后两种方法,后两种方法要自己构造数组或者集合。

个人认为上述执行查询方式跟java.sql.PreparedStatement is almost all about assigning a value to a specified parameter, such as P1,p2,p3,array, map, where the values of the parameters that need to be specified are stored, as in the order in which they are preparedstatement and in which the parameters are defined.

It's to be noted that

The order of the arrays in the L- array parameter collection is defined in the order of the parameters.

L The key in the map parameter collection is the name of the parameter and value is the parameter

The top has started to refer to queries with parameters, yes, the query interface defines methods for introducing parameters:

Public void declareParameters(java.lang.String parameters)

如果事多了参数的情况要用逗号隔开。

 

JDO的查询方式不光可以引入参数,同时可以引入变量以及其他的类,下面分别是引入变量和引入类的方法,其语法和一般JAVA语法没什么差别:

    引入变量,当引入多个变量的时候中间用分号隔开

 

    public void declareVariables(java.lang.String  variables )

    例子:public void declareVariables(“Student students;Teacher teacher1”);

 

    引入类,当引入多个类的时候,跟字符串的连接方式差不多,不好说明,看例子吧:

public void declareImports(java.lang.String  imports )

例子:public void declareImports(“import java.util.Date;”+”import com.yourname.Student”);

now that the query is going to exist, query interface defines a method for filtering conditions:serfilter ()

public void SetFilter (java.lang.String filter)

The method of the filter is the same as that of the conditional writing in the WHERE statement in SQL , for example, if the query condition is: To find the student surnamed Li, then the wording is as follows:

Query.setfilter ("Name.startswith (StartName)");

And at the same time find the gender equal to the female, the wording should read:

Query.setfilter ("Name.startswith (startname) && sex==it_sex");

How do I see StartsWith and EndsWith, in JDO, about string manipulation functions, So how do I make a more ambiguous query about strings? The advice?

A Sort method is also provided in the JDO query,setordering as follows:

public void setordering (java.lang.String ordering)

in the order of the student's age:

Query.setordering ("Age Ascending"); Ascending

Query.setordering ("Age descending"); Descending

if more than one sort condition, such as by age, size, Height:

Query.setordering ("Age Ascending," + "stature descending");

Java. Jdo. Extend

========================

an instance of this interface represents all the class objects that exist in the current database. It is also created by the interface PersistenceManager , as follows:

Public Extent getextent (Class Persistencecapableclass, Boolean subclasses)

The true and false representation of the parameter subclass contains subclasses of the first argument class: True, the representation includes subclasses, false, excluding subclasses.

There may be two places to use the Extend instance .

L Get a set of specific persistent classes.

• query for all specific persisted classes in the database

-->
-->

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.