The example class specifies how to build a dynamic where clause. Each non-BLOB column in the table can optionally be included in the where clause. Examples are the best way to demonstrate the usage of this class.
The example class can be used to generate a virtually unlimited where clauses.
The example classes contain an inner static class called Criteria that holds a list of conditions that will be anded together in the where clause. The example class holds a list of Criteria objects and all the clauses from the inner classes will be ored together. Using different sets of Criteria classes allows you to generate virtually unlimited types of where clauses.
Criteria objects can be created with the either the createCriteria method or the or method in the example class. When the first Criteria object is created with the createCriteria method it is automatically added to the list of Criteria objects - this makes it easy to write a simple where clause if you don't need to or several other clauses together. When using the or method, the Criteria class is added to the list in all instances.
Important We recommend that you only use the or method for creating Criteria classes. We believe that this method makes for more readable code.
This example shows how to generate a simple WHERE clause using the generated example class:
TestTableExample example = new TestTableExample(); example.createCriteria().andField1EqualTo(5);
Alternatively, the following syntax also works:
TestTableExample example = new TestTableExample(); example.or().andField1EqualTo(5);
In either above example, the dynamically generated where clause will effectively be:
where field1 = 5
The next example shows how to generate a complex WHERE clause using the generated example class (using JSE 5.0 parameterized types):
TestTableExample example = new TestTableExample(); example.or() .andField1EqualTo(5) .andField2IsNull(); example.or() .andField3NotEqualTo(9) .andField4IsNotNull(); List<Integer> field5Values = new ArrayList<Integer>(); field5Values.add(8); field5Values.add(11); field5Values.add(14); field5Values.add(22); example.or() .andField5In(field5Values); example.or() .andField6Between(3, 7);
In the above example, the dynamically generated where clause will effectively be:
where (field1 = 5 and field2 is null) or (field3 <> 9 and field4 is not null) or (field5 in (8, 11, 14, 22)) or (field6 between 3 and 7)
Returned records will meet these criteria.
You can force queries to be DISTINCT by calling the setDistinct(true) method on any example class.
The Criteria inner class includes andXXX methods for each field, and each standard SQL predicate including: