User's Manual

Chapter 9. Persistence Tutorial 81
}
To retrieve all users whose last name is "Smith", do the following:
DataQuery query = session.retrieveQuery("UsersGroups");
query.addEqualsFilter("lastName", "Smith")
while (query.next()) {
System.out.println("First name = " + query.get("firstName") +
"; Last name = " + query.get("lastName") +
"; Group = " + query.get("groupName"));
}
To get all users whose last name starts with "S", use the addFilter method:
DataQuery query = session.retrieveQuery("UsersGroups");
// FilterFactory is explained below
query.addFilter\
(query.getFilterFactory().startsWith("lastName", "S", false));
while (query.next()) {
System.out.println("First name = " + query.get("firstName") +
"; Last name = " + query.get("lastName") +
"; Group = " + query.get("groupName"));
}
9.5.1.3. Complex Filters
For more complex queries, it is helpful to understand the role of each interface that deals with Fil-
ters.
Filter This class represents a single expression for part of a "where" clause. For instance, a
Filter could be "foo = :bar" with a value associated with "bar" (e.g. "foo = 3").
CompoundFilter This class extends Filter and provides the ability to add filters together
using the AND and OR keywords.
FilterFactory This class is responsible for handing out filters. It has methods such as "sim-
ple", "equals", "notEquals", "lessThan", "greaterThan", "startsWith", "contains", and "endsWith".
If a user is using Oracle or Postgres, all these methods check whether the value is null, and if so,
act correctly (e.g., use "foo is null" instead of "foo = null").
DataQuery This class allows you to add filters as well as get a reference to the FilterFactory. If
you need a FilterFactory but do not have a DataQuery, use Session.getFilterFactory().
If you want to filter the query based on certain conditions, you can incrementally build up your query
as follows:
DataQuery query = session.retrieveQuery("UsersGroups");
FilterFactory factory = query.getFilterFactory();
if (beginLetter != null) {
query.addFilter(factory.lessThan("firstLetter", beginLetter, true));
}
if (lastLetter != null) {
query.addFilter(factory.greaterThan("firstLetter", beginLetter, true));
}
while (query.next()) {