JPOX
JPOX
JPOX  |  Version 1.0  |  Version 1.1  |  Version 1.2  |  JDO  |  JPA 
1.2 | Persistence | JDO ORM | JPA ORM | Query | Runtime | Extensions | Tutorials and Examples
Queries
JDOQL : Methods

When writing the "filter" for a JDOQL Query you can make use of some methods on the various Java types. The range of methods included as standard in JDOQL is not as flexible as with the true Java types, but the ones that are available are typically of much use. In addition, JPOX adds on many extensions to the JDO specifications providing all of the commonly required methods. Below is a list of what is required by JDOQL in JDO 1.0 and JDO 2.0, and what JPOX supports.

Java TypeMethodDescriptionSpecificationJPOX support
StringstartsWith(String)Returns if the string starts with the passed stringJDO 1.0, JDO 2.0
StringendsWith(String)Returns if the string ends with the passed stringJDO 1.0, JDO 2.0
StringindexOf(String)Returns the first position of the passed stringJDO 2.0
StringindexOf(String,int)Returns the position of the passed string, after the passed positionJDO 2.0
Stringsubstring(int)Returns the substring starting from the passed positionJDO 2.0
Stringsubstring(int,int)Returns the substring between the passed positionsJDO 2.0
StringtoLowerCase()Returns the string in lowercaseJDO 2.0
StringtoUpperCase()Retuns the string in UPPERCASEJDO 2.0
Stringmatches(String pattern)Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method.JDO 2.0
Stringlike(String pattern)Returns whether string matches the passed expression. The pattern argument is database specific.
StringcharAt(int)Returns the character at the passed position
StringstartsWith(String,int)Returns if the string starts with the passed string, from the passed position
Stringlength()Returns the length of the string
Stringequals(String)Returns if the string is equals to the passed string
Stringtrim()Returns a trimmed version of the string
CollectionisEmpty()Returns whether the collection is emptyJDO 1.0, JDO 2.0
Collectioncontains(value)Returns whether the collection contains the passed elementJDO 1.0, JDO 2.0
Collectionsize()Returns the number of elements in the collectionJDO 2.0
MapisEmpty()Returns whether the map is emptyJDO 1.0, JDO 2.0
Mapcontains(value)Returns whether the map contains the passed value
MapcontainsKey(key)Returns whether the map contains the passed keyJDO 2.0
MapcontainsValue(value)Returns whether the map contains the passed valueJDO 2.0
MapcontainsEntry(key,value)Returns whether the map contains the passed entry
Mapget(key)Returns the value from the map with the passed keyJDO 2.0
Mapsize()Returns the number of entries in the mapJDO 2.0
Mathabs(number)Returns the absolute value of the passed numberJDO 2.0
Mathsqrt(number)Returns the square root of the passed numberJDO 2.0
Mathcos(number)Returns the cosine of the passed number
Mathsin(number)Returns the absolute value of the passed number
Mathtan(number)Returns the tangent of the passed number
Mathacos(number)Returns the arc cosine of the passed number
Mathasin(number)Returns the arc sine of the passed number
Mathatan(number)Returns the arc tangent of the passed number
Mathexp(number)Returns the exponent of the passed number
Mathlog(number)Returns the log(base e) of the passed number
Mathfloor(number)Returns the floor of the passed number
Mathceil(number)Returns the ceiling of the passed number
DategetDay()Returns the day (of the month) for the date
DategetMonth()Returns the month for the date
DategetYear()Returns the year for the date
TimegetHour()Returns the hour for the time
TimegetMinute()Returns the minute for the time
TimegetSecond()Returns the second for the time
JDOHelpergetObjectId(object)Returns the object identity of the passed persistent objectJDO 2.0
Analysisrollup({object})Perform a rollup operation over the results.
Analysiscube({object})Perform a cube operation over the results.
{}lengthReturns the length of an array
{}contains(object)Returns true if the array contains the object


The following sections provide some examples of what can be done using JDOQL methods.



Example 1 - Map methods (I)

Here's another example using the same Product class as a value in a Map. This introduces how you query Collection and Map fields using the operations available. Collections and Maps act very similarly. Our example represents an organisation that has several Inventories of products. Each Inventory of products is stored using a Map, keyed by the Product name. The query searches for all Inventories that contain a product with the name "product 1".

Declarative JDOQL :
Extent e=pm.getExtent(org.jpox.samples.store.Inventory.class,false);
Query query = pm.newQuery(e,"products.containsKey(\"product 1\")");
List results = (List)query.execute();

Single-String JDOQL :
Query query = pm.newQuery("SELECT FROM org.jpox.samples.store.Inventory EXCLUDE SUBCLASSES " +
                "WHERE products.containsKey(\"product 1\")");
List results = (List)query.execute();

Here's the source code for reference

class Inventory
{
    Map products;
    ...
}
class Product
{
    String name;
    double price;
    double salePrice;
    java.util.Date endDate;
    String[] composition;
    ...
}

<jdo>
    <package name="org.jpox.samples.store">
        <class name="Inventory">
            <field name="products">
                <map key-type="java.lang.String" value-type="org.jpox.samples.store.Product"/>
                <key mapped-by="name"/>
            </field>
        </class>

        <class name="Product">
            <field name="name">
                <column length="100" jdbc-type="VARCHAR"/>
            </field>
            <field name="price"/>
            <field name="endDate"/>
        </class>
    </package>
</jdo>


Example 2 - Map methods (II)

We might want to to check if a Collection field contains one or other elements. We extend the previous example that is using the Product class as a value in a Map. Our example represents an organisation that has several Inventories of products. Each Inventory of products is stored using a Map, keyed by the Product name. The query searches for all Inventories that contain a product with the name "product 1" or "product 2".

Declarative JDOQL :
Extent e=pm.getExtent(org.jpox.samples.store.Inventory.class,false);
Query query = pm.newQuery(e);
query.declareVariables("String productName");
query.setFilter("products.containsKey(productName) && (productName==\"product 1\" || productName==\"product 2\")");
List results = (List)query.execute();

Single-String JDOQL:
Query query = pm.newQuery("SELECT FROM org.jpox.samples.store.Inventory EXCLUDE SUBCLASSES " + 
    "WHERE products.containsKey(productName) && (productName==\"product 1\" || productName==\"product 2\") " + 
    "VARIABLES String productName");
List results = (List)query.execute();


Example 3 - String.startsWith() method

Here's another example using the same Product class as above, but this time looking for objects which their abreviation is the begin of a trade name. The trade name is provided as parameter.

Declarative JDOQL :
Query query = pm.newQuery(org.jpox.samples.store.Product.class);
query.declareImports("import java.lang.String");
query.declareParameters("java.lang.String tradeName");
query.setFilter("tradeName.startsWith(this.abbreviation)");
List results = (List)query.execute("Workbook Advanced");

Single-String JDOQL :
Query query = pm.newQuery("SELECT FROM org.jpox.samples.store.Product " +
                "WHERE tradeName.startsWith(this.abbreviation) " +
                "PARAMETERS java.lang.String tradeName import java.lang.String");
List results = (List)query.execute("Workbook Advanced");


Example 4 - Date.getYear() method

Here's an example using the Sale's in a store, where all Sale's are persisted with a date. We want to find all sales in a particular year. Clearly we dont have a field called "year", but we can make use of the JPOX extension "Date.getYear()" method

Declarative JDOQL :
Query query = pm.newQuery(org.jpox.samples.store.Sale.class);
query.declareParameters("int theYear");
query.setFilter("date.getYear() == theYear");
List results = (List)query.execute(new Integer(2004));

Single-String JDOQL :
Query query = pm.newQuery("SELECT FROM org.jpox.samples.store.Sale " +
                "WHERE date.getYear() == theYear" +
                "PARAMETERS int theYear");
List results = (List)query.execute(new Integer(2004));


Example 5 - Using contains with arrays

Here's an example using the Product and checking if one element is part of the Product composition. Note that Production.composition is of type String[].

Query query = pm.newQuery(org.jpox.samples.store.Product.class);
query.setFilter("this.composition.contains(element)");
query.declareParameters("String element");
List results = (List)query.execute("sugar");


Example 6 - Defining arrays

Here's an example of array definition using curly braces {} in the query. In this example we check that the Product can be easily manufactured by verifying the number of elements in the composition. Note that Production.composition is of type String[].

Query query = pm.newQuery(org.jpox.samples.store.Product.class);
query.setFilter("{'Candy','Ice cream'}.contains(this.name) && this.composition.length < 5");
List results = (List)query.execute();


Example 7 - Analysis.rollup() method

Here's an example using the Product in a store and the rollup method. We want to find the max price of a product and all products at given end dates.

Query query = pm.newQuery(org.jpox.samples.store.Product.class);
query.setResult("name, endDate, max(price)");
query.setGrouping("Analysis.rollup({name,endDate})");
List results = (List)query.execute();