More Books
Hibernate: A J2EE Developer's Guide
Hibernate: A J2EE™ Developer's Guide
Table of Contents
Copyright
Acknowledgments
About the Author
Preface
Required Skills
Roadmap
Chapter 1. Overview
Why Object/Relational Mapping?
What Is Hibernate?
Comparing JDBC to Hibernate
Hibernate's Mapping System
Other Java/Database Integration Solutions
How to Obtain and Install
Supported Databases
Chapter 2. Getting Oriented
Application Architecture
Mapping Files
Generating Java Source
Application Configuration
Web Application
JSP Interface
Chapter 3. Starting from Java
Java Object Model
Generated Mapping Files
Generated Schema
Working with Artifacts and Owners
Chapter 4. Starting from an Existing Schema
Initial Schema
Using Middlegen
Generated Mapping Files
Generated Java
Working with the Database
Chapter 5. Mapping Files
Basic Structure
Mapping File Reference
Chapter 6. Persistent Objects
Sessions
Objects and Identity
Life-Cycle Methods
Chapter 7. Relationships
Database Relationships
Java Collection Relationships
Java Class Relationships
Any-Based Relationships
Bi-directional Relationships
Chapter 8. Queries
HQL
HQL Reference
Select
From
Where
Group By
Having
Order By
Criteria Queries
Native SQL Queries
Chapter 9. Transactions
Introduction to Transactions
Optimistic and Pessimistic Locking
Chapter 10. Performance
Finding and Solving Problems
Queries
Inserts
Connection Pooling
Caching
Chapter 11. Schema Management
Updating an Existing Schema
Generating Update and Drop Scripts
Chapter 12. Best Practices, Style Guide, Tips and Tricks
Reducing Code with Inversion of Control
Reducing Session Creation Impact with ThreadLocal
Using Hibernate as an EJB BMP Solution
Integrating with Other Technologies
Applications That Use Hibernate
Strategies for Getting Started
Chapter 13. Future Directions
Hibernate 3.0
EJB 3.0
Here and Now
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X

Criteria Queries

Hibernate supports the Criteria API, an object-based query mechanism that constructs a query from a set of objects. The Criteria API is perhaps best understood in the context of an ordinary HQL statement, as described earlier in this chapter.

HQL Clause

Criteria Equivalent[s]

select

Not available

from

Criteria criteria = session.create

Criteria(Student.class);

from joins

Criteria.setFetchMode()for associations.

where

net.sf.hibernate.expression.Expression to obtain built-in net.sf.hibernate.expression.Criterion

group by

Not available

having

Not available

order by

Criteria.addOrder() to add net.sf.hibernate.expression.Order


The Criteria interface, as shown in Figure 8.12, is returned by a simple Session.createCriteria() call.

Figure 8.12. Criteria Interface


Normally, you will use the Expression factory, as shown in Figure 8.13, to obtain a criterion that you wish to apply.

Figure 8.13. Expression Factory


Figure 8.14 shows the hierarchy of built-in criteria. Normally, you won't instantiate these directly, and instead will use the built-in Expression factory.

Figure 8.14. Built-in Criteria Hierarchy


The code shown in Listing 8.3 returns the same result set as the HQL select student.firstName from Student as student where student.firstName like 'B%'.

Listing 8.3. Criteria Query
Criteria myQuery =
       hibernateSession.createCriteria(Student.class);
myQuery.add(Expression.like("firstName", "B%"));

Method Chaining

In addition to the standard method execution shown in Listing 8.3, the Criteria API supports the notion of method chaining. Simply put, most of the methods on the Criteria interface return the calling object (i.e., they conclude with the statement return this). This allows you to rewrite the code shown in Listing 8.3 as shown in Listing 8.4.

Listing 8.4. Chained Criteria Query
Criteria myQuery =
     hibernateSession.createCriteria(Student.class).add(
          Expression.like("firstName", "B%"));

As can be seen, this may lead to different (but equivalent) ways of expressing more complex statements. For example, the code shown in Listing 8.5 is equivalent to the code in Listing 8.6.

Listing 8.5. Complex Criteria
Criteria myQuery =
                hibernateSession.createCriteria(Student.class);
myQuery.add(Expression.like("firstName", "B%"));
myQuery.add(Expression.like("lastName", "S%"));
myQuery.setFetchMode("examresults", FetchMode.EAGER);
myQuery.setMaxResults(10);
myQuery.setTimeout(1000);

Listing 8.6. Complex Criteria with Method Chaining
Criteria myQuery =
     hibernateSession
          .createCriteria(Student.class)
          .add(Expression.like("firstName", "B%"))
          .add(Expression.like("lastName", "S%"))
          .setFetchMode("examresults", FetchMode.EAGER)
          .setMaxResults(10)
          .setTimeout(1000);

In certain instances, the more verbose method shown in Listing 8.5 is easier to understand and debug, but the choice of syntax is largely a matter of style.

Easily Override Lazy Settings

One helpful use of the Criteria API is the ability to easily override a lazy="true" setting in a Hibernate mapping with the use of the setFetchMode() method. For example, as shown in Listing 8.6, the method setFetchMode()overrides the lazy="true" setting in the Student mapping of the examresults collection.

Put another way, the setFetchMode() is a convenient way to express an HQL join statement.