Using ORM Criteria
ORM Criteria is one of the handy approaches that aids data retrieval in writing a program. Instead of writing a lot of if-then-else in checking and filtering the records you need, ORM Criteria groups all the required conditions as a single criteria object, and you can retrieve data by loading the records from that criteria object.
In order to use ORM Criteria, make sure you have checked Generate Criteria in the Database Code Generation window.
Checking Generate Criteria in Database Code Generation window |
Understanding the Criteria class
The following is an example of ORM Criteria class. Its name and attributes respect the corresponding ORM Persistable class in your object model.
public final StringExpression name;
public final IntegerExpression age;
public final CharacterExpression gender;
public final DateExpression dob;
public final IntegerExpression ID;
public StaffCriteria(PersistentSession session) {
super(session.createCriteria(Staff.class));
name = new StringExpression("name", this);
age = new IntegerExpression("age", this);
gender = new CharacterExpression("gender", this);
dob = new DateExpression("dob", this);
ID = new IntegerExpression("ID", this);
}
public StaffCriteria() throws PersistentException {
this(com.UntitledPersistentManager.instance().getSession());
}
public Staff uniqueStaff() {
return (Staff) super.uniqueResult();
}
public Staff[] listStaff() {
return (Staff[]) super.list().toArray(new Staff[super.list().size()]);
}
}
Using ORM Criteria in programming
To use an ORM Criteria, you need to first specify the conditions and then retrieve data from the Criteria class. To specify conditions, you need to write the following in source code:
where criteria is the instance of the criteria class; property is the property of the criteria; expression is the expression to be applied on the property; parameter is the parameter(s) of the expression. The table below shows the expression that can be used for specifying the condition for query.
|
||||||||||||||||||||||||||||||
Expression of ORM Criteria |
Here is an example of specifying conditions with ORM criteria:
There are two types of ordering to sort the retrieved records, that is, ascending and descending order. To sort the retrieved records with respect to the property, use the code template:
where the value of ascending_order is either true or false. True refers to sort the property in ascending order while false refers to sort the property in descending order. For example:
To set the range of the number of records to be retrieved by using one of the two methods:
- setFirstResult(int i) - Retrieve the i-th record from the results as the first result.
- setMaxResult(int i) - Set the maximum number of retrieved records by specified value, i.
For example:
The StaffCriteria class contains two methods to load the retrieved record(s) to an object or array.
- uniqueClass() - Retrieve a single record matching the specified condition(s) for the criteria; Exception will be thrown if the number of retrieved record is not equal to 1.
- listClass() - Retrieve the records matched with the specified condition(s) for the criteria.
For example:
Comparison between Criteria class and SQL query
Both SQL Query and Criteria Class can help you find records from databas. However, SQL Query is usually long and complex. It is easy to make syntax mistake when writing SQL Query and when a mistake happens, it is hard to debug, too. On the contrary, Criteria class is easy to use. It also supports getting persistent objects directly from database while SQL Query can only retrieve individual data from database.
|
||||||||||
Comparison between Criteria class and SQL Querying |
Related Resources
The following resources may help you to learn more about the topic discussed in this page.
5. Using ORM Qualifier | Table of Contents | Part XI. Advanced modeling toolset |