|
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. |
||
|
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.
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:
-
criteria.prop.expression(param);where criteria is the criteria instance, prop is the property to be restricted, expression is the expression to apply, and param is the parameter for the expression.
The following table is a list of available expression: (Note: Some expression applies to particular data types only)
Expression
Description
eq(value)
Property equal to value
ne(value)
Property not equal to value
gt(value)
Property greater than value
ge(value)
Property greater than or equal to value
lt(value)
Property less than value
le(value)
Property less than or equal to value
isEmpty()
Property is empty
isNotEmpty()
Property is not empty
isNull()
Property is NULL
isNotNull()
Property is not NULL
in(values)
Property contains in values array
between(value1, value2)
Property value is between value1 and value2
like(value)
String property like value, use '%' in value for wildcard
ilike(value)
Case-insensitive like
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:
Method
Description
setFirstResult(i)
Set the first result to be retrieved
setMaxResults(i)
Limit the number of result to be retrieved
Two methods are available to execute the query and return the result(s):
Method
Description
uniqueClass()
Return a single result, will throw exception if result count <> 1
listClass()
Return an array of result
where Class is the name of the ORM Persistable Class.
Examples
Assuming you have the following data in your database:
ID
name
gender
dob
type
1
Henry Chan
M
1967-06-23
1
2
Amy Wong
F
1983-01-03
2
3
Louie Kwan
M
1977-04-18
3
Select a single member with name = "Amy Wong"
MemberCriteria criteria = new MemberCriteria(); criteria.name.eq("Amy Wong"); Member member = criteria.uniqueMember(); System.out.println(member);
System Output (Assuming you selected override toString() method with all properties option)
Member[ ID=2 Name=Amy Wong Gender=F Dob=1983-01-03 Type=2 ]
Select all members whose DOB is between 1960-01-01 and 1980-01-01:
MemberCriteria criteria = new MemberCriteria(); criteria.dob.between(new GregorianCalendar(1960, 1, 1).getTime(), new GregorianCalendar(1980, 1, 1).getTime()); Member[] members = criteria.listMember(); for (int i = 0; i < members.length; i++) { System.out.println(members[i]); }
System Output
Member[ ID=1 Name=Henry Chan Gender=M Dob=1967-06-23 Type=1 ]
Member[ ID=3 Name=Louie Kwan Gender=M Dob=1977-04-18 Type=3 ]
Select all golden and silver members:
MemberCriteria criteria = new MemberCriteria(); criteria.type.in(new int[] {Member.TYPE_GOLDEN, Member.TYPE_SILVER}); Member[] members = criteria.listMember(); for (int i = 0; i < members.length; i++) { System.out.println(members[i]); }
System Output
Member[ ID=1 Name=Henry Chan Gender=M Dob=1967-06-23 Type=1 ]
Member[ ID=2 Name=Amy Wong Gender=F Dob=1983-01-03 Type=2 ]
Select all members with gender='M', type is not silver; and sort the retrieved records by DOB in ascending order:
MemberCriteria criteria = new MemberCriteria(); criteria.gender.eq('M'); criteria.type.ne(Member.TYPE_SILVER); criteria.dob.order(true); Member[] members = criteria.listMember(); for (int i = 0; i < members.length; i++) { System.out.println(members[i]); }
System Output
Member[ ID=1 Name=Henry Chan Gender=M Dob=1967-06-23 Type=1 ]
Member[ ID=3 Name=Louie Kwan Gender=M Dob=1977-04-18 Type=3 ]
Related Articles
Learn how to use the generated Java persistence class to manipulate the database by static method persistent API
Using Java Persistence Class with Static Method Persistent API
Learn how to use the generated Java persistence class to manipulate the database by factory class persistent API
Using Java Persistence Class with Factory Class Persistent API
Learn how to use the generated Java persistence class to manipulate the database by POJO persistent API
Using Java Persistence Class with POJO Persistent API
Learn how to use the generated Java persistence class to manipulate the database by DAO persistent API
Using Java Persistence Class wtih DAO Persistent API
Resources
Know more about DB Visual ARCHITECT - Java Persistent Code Generator for Relational Database
http://www.visual-paradigm.com/product/dbva/
Look at the features of DB Visual ARCHITECT
http://www.visual-paradigm.com/product/dbva/features/
Get the Free 30 Days Trial of DB Visual ARCHITECT
http://www.visual-paradigm.com/download/
Browse the DB Visual ARCHITECT's online documentation
http://www.visual-paradigm.com/documentation/dbvauserguide.jsp
Chapter 7 Implementation - This chapter shows you how to generate the persistent code and describes how the POJO Model maps to ORM-Persistent Class
Chapter 8 Manipulating Persistent Data - This chapter shows how to use the generated persistent code for inserting, retrieving, updating and deleting persistent data and demonstrates how to run the generated sample code.
View DB Visual ARCHITECT's Interactive Tutorials
http://www.visual-paradigm.com/product/dbva/tutorials/