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. While DataNucleus supports all of the methods in the JDO standard, it also supports several yet to be standardised (extension) method.
Please note that you can easily add support for other methods for evaluation "in-memory" using this
DataNucleus plugin point
Please note that you can easily add support for other methods with RDBMS datastore using this
DataNucleus plugin point
| Method | Description | Standard |
|---|---|---|
| startsWith(String) | Returns if the string starts with the passed string | |
| endsWith(String) | Returns if the string ends with the passed string | |
| indexOf(String) | Returns the first position of the passed string | |
| indexOf(String,int) | Returns the position of the passed string, after the passed position | |
| substring(int) | Returns the substring starting from the passed position | |
| substring(int,int) | Returns the substring between the passed positions | |
| toLowerCase() | Returns the string in lowercase | |
| toUpperCase() | Retuns the string in UPPERCASE | |
| matches(String pattern) | Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method. | |
| charAt(int) | Returns the character at the passed position | |
| startsWith(String, int) | Returns if the string starts with the passed string, from the passed position | |
| length() | Returns the length of the string | |
| trim() | Returns a trimmed version of the string | |
| equals(String) | Returns if the strings are equal | |
| equalsIgnoreCase(String) | Returns if the strings are equal ignoring case | |
| replaceAll(String, String) | Returns the string with all instances of str1 replaced by str2 | |
| trimLeft() | Returns a trimmed version of the string (trimmed for leading spaces). Only on RDBMS | |
| trimRight() | Returns a trimmed version of the string (trimmed for trailing spaces) Only on RDBMS |
Here's an example using a Product class, looking for objects which their abreviation is the beginning of a trade name. The trade name is provided as parameter.
Declarative JDOQL :
Query query = pm.newQuery(mydomain.Product.class);
query.setFilter(":tradeName.startsWith(this.abbreviation)");
List results = (List)query.execute("Workbook Advanced");
Single-String JDOQL :
Query query = pm.newQuery(
"SELECT FROM mydomain.Product " +
"WHERE :tradeName.startsWith(this.abbreviation)");
List results = (List)query.execute("Workbook Advanced");| Method | Description | Standard |
|---|---|---|
| isEmpty() | Returns whether the collection is empty | |
| contains(value) | Returns whether the collection contains the passed element | |
| size() | Returns the number of elements in the collection | |
| get(int) | Returns the element at that position of the List |
Here's an example demonstrating use of contains(). We have an Inventory class that has a Collection of Product objects, and we want to find the Inventory objects with 2 particular Products in it. Here we make use of a variable (prd to represent the Product being contained
Declarative JDOQL :
Query query = pm.newQuery(mydomain.Inventory.class);
query.setFilter("products.contains(prd) && (prd.name==\"product 1\" || prd.name==\"product 2\")");
List results = (List)query.execute();
Single-String JDOQL:
Query query = pm.newQuery(
"SELECT FROM mydomain.Inventory EXCLUDE SUBCLASSES " +
"WHERE products.contains(prd) && (prd.name==\"product 1\" || prd.name==\"product 2\")");
List results = (List)query.execute();| Method | Description | Standard |
|---|---|---|
| isEmpty() | Returns whether the map is empty | |
| containsKey(key) | Returns whether the map contains the passed key | |
| containsValue(value) | Returns whether the map contains the passed value | |
| get(key) | Returns the value from the map with the passed key | |
| size() | Returns the number of entries in the map | |
| containsEntry(key, value) | Returns whether the map contains the passed entry |
Here's an example using a 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".
Declarative JDOQL :
Query query = pm.newQuery(mydomain.Inventory.class, "products.containsKey(\"product 1\")");
List results = (List)query.execute();
Single-String JDOQL :
Query query = pm.newQuery(
"SELECT FROM mydomain.Inventory EXCLUDE SUBCLASSES " +
"WHERE products.containsKey(\"product 1\")");
List results = (List)query.execute();Here's the source code for reference
class Inventory
{
Map<String, Product> products;
...
}
class Product
{
...
}| Method | Description | Standard |
|---|---|---|
| getDay() | Returns the day (of the month) for the date (java.util.Date types) | |
| getMonth() | Returns the month for the date (java.util.Date types) | |
| getYear() | Returns the year for the date (java.util.Date types) | |
| getHour() | Returns the hour for the time (java.util.Date types) | |
| getMinute() | Returns the minute for the time (java.util.Date types) | |
| getSecond() | Returns the second for the time (java.util.Date types) |
| Method | Description | Standard |
|---|---|---|
| ordinal() | Returns the ordinal of the enum (not implemented for enum expression when persisted as a string) | |
| toString() | Returns the string form of the enum (not implemented for enum expression when persisted as a numeric) |
| Class | Method | Description |
|---|---|---|
| java.awt.Point | getX() | Returns the X coordinate. Only on RDBMS |
| java.awt.Point | getY() | Returns the Y coordinate. Only on RDBMS |
| java.awt.Rectangle | getX() | Returns the X coordinate. Only on RDBMS |
| java.awt.Rectangle | getY() | Returns the Y coordinate. Only on RDBMS |
| java.awt.Rectangle | getWidth() | Returns the width. Only on RDBMS |
| java.awt.Rectangle | getHeight() | Returns the height. Only on RDBMS |
| org.joda.time.Interval | getStart() | Returns the start date/time object. Only on RDBMS |
| org.joda.time.Interval | getEnd() | Returns the end date/time object. Only on RDBMS |
| {} | length | Returns the length of an array. Only on RDBMS |
| {} | contains(object) | Returns true if the array contains the object. Only on RDBMS |
| Method | Description | Standard |
|---|---|---|
| Math.abs(number) | Returns the absolute value of the passed number | |
| Math.sqrt(number) | Returns the square root of the passed number | |
| Math.cos(number) | Returns the cosine of the passed number | |
| Math.sin(number) | Returns the absolute value of the passed number | |
| Math.tan(number) | Returns the tangent of the passed number | |
| Math.acos(number) | Returns the arc cosine of the passed number | |
| Math.asin(number) | Returns the arc sine of the passed number | |
| Math.atan(number) | Returns the arc tangent of the passed number | |
| Math.ceil(number) | Returns the ceiling of the passed number | |
| Math.exp(number) | Returns the exponent of the passed number | |
| Math.floor(number) | Returns the floor of the passed number | |
| Math.log(number) | Returns the log(base e) of the passed number | |
| JDOHelper.getObjectId(object) | Returns the object identity of the passed persistent object | |
| JDOHelper.getVersion(object) | Returns the version of the passed persistent object | |
| SQL_rollup({object}) | Perform a rollup operation over the results. Only for some RDBMS e.g DB2, MSSQL, Oracle | |
| SQL_cube({object}) | Perform a cube operation over the results. Only for some RDBMS e.g DB2, MSSQL, Oracle | |
| SQL_boolean({sql}) | Embed the provided SQL and return a boolean result. Only on RDBMS | |
| SQL_numeric({sql}) | Embed the provided SQL and return a numeric result. Only on RDBMS |