Using Criteria Class

Criteria class is a helper class for querying database without writing query language. It simplifies the way to query data from database.

Contents

Criteria class will be generated for each ORM Persistable Class. The parameters and returned object are strong type so casting is not necessary. It is similar to ORM Qualifier but it is more flexiable, and supports different combination of property, expression and ordering.

Generating Criteria Class

Using Criteria Class

For example, searching members for those who are male, aged 18 or above and sorting the member records by name

The rest of this article will show you how to generate criteria class and how to use it with examples.

Generating Criteria Class

1. Let's assume you have the following class diagram:

2. Synchronize the class diagram to ERD and generate code by using the Database Code Generation dialog box. On the Database Code Generation dialog box, check the Generate Criteria option as follow:

An extra source file MemberCriteria.java is generated.

Top

Using Criteria Class

Usage

A property will be generated in criteria class corresponding to each simple property in ORM Persistable class. To use the criteria class, first create an instance of criteria class, then apply restriction to properties, finally get a single or array of result.

To apply restriction to property, simply call:

The following table is a list of available expression: (Note: Some expression applies to particular data types only)

A special method order(ascending) is used to sort the property in ascending (ascending = true) or descending (ascending = false) order. All these restrictions and orders can be applied to the same criteria instance.

The following methods limit the range of result to be retrieved:

Two methods are available to execute the query and return the result(s):

Examples

Assuming you have the following data in your database:

Top

Related Articles

Top

Resources

Top

last edited 2005-10-18 08:45:00 by Author6