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. This document defines the standard methods available in JDO2. If you look at the datastore-specific implementations you can find some extensions to this list (particularly for RDBMS).

Java Type Method Description Specification
String startsWith(String) Returns if the string starts with the passed string JDO 1.0, JDO 2.0
String endsWith(String) Returns if the string ends with the passed string JDO 1.0, JDO 2.0
String indexOf(String) Returns the first position of the passed string JDO 2.0
String indexOf(String,int) Returns the position of the passed string, after the passed position JDO 2.0
String substring(int) Returns the substring starting from the passed position JDO 2.0
String substring(int,int) Returns the substring between the passed positions JDO 2.0
String toLowerCase() Returns the string in lowercase JDO 2.0
String toUpperCase() Retuns the string in UPPERCASE JDO 2.0
String matches(String pattern) Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method. JDO 2.0
Collection isEmpty() Returns whether the collection is empty JDO 1.0, JDO 2.0
Collection contains(value) Returns whether the collection contains the passed element JDO 1.0, JDO 2.0
Collection size() Returns the number of elements in the collection JDO 2.0
Map isEmpty() Returns whether the map is empty JDO 1.0, JDO 2.0
Map containsKey(key) Returns whether the map contains the passed key JDO 2.0
Map containsValue(value) Returns whether the map contains the passed value JDO 2.0
Map get(key) Returns the value from the map with the passed key JDO 2.0
Map size() Returns the number of entries in the map JDO 2.0
Math abs(number) Returns the absolute value of the passed number JDO 2.0
Math sqrt(number) Returns the square root of the passed number JDO 2.0
JDOHelper getObjectId(object) Returns the object identity of the passed persistent object JDO 2.0


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.datanucleus.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.datanucleus.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.datanucleus.samples.store">
        <class name="Inventory">
            <field name="products">
                <map key-type="java.lang.String" value-type="org.datanucleus.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.datanucleus.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.datanucleus.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.datanucleus.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.datanucleus.samples.store.Product " +
                "WHERE tradeName.startsWith(this.abbreviation) " +
                "PARAMETERS java.lang.String tradeName import java.lang.String");
List results = (List)query.execute("Workbook Advanced");