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

Select

SELECT DISTINCT? selectedPropertiesList | ( NEW className OPEN selectedPropertiesList CLOSE );

The optional distinct keyword can be used to reduce the result set to unique results (no result is returned more than once). This can be useful when you wish to limit the results. For example, the statement select student.id from Student as student, Student as student2 returns the full product of every record in the Student table as matched with every other student record (literally, the records in the student table multiplied by the records in the student table). The distinct keyword can be used to limit these results back to the individual student records. For example, the statement select distinct student.id from Student as student, Student as student2 will return the same results as a from Student.

If one or more properties (selectedProperties) are specified, the query will return them as mapped to objects. For example, the HQL select owner.id, owner.name from com.cascadetg.ch03.Owner as owner will return a set of java.lang.Long and java.lang.String objects.

Instead of specifying that the properties should be returned as a set of different objects, you may wish to use the new className statement instead. This will return an arbitrary class, calling the constructor as the result of a query. For example, the HQL command select new java.lang.StringBuffer (owner.name) from Owner as owner returns a set of java.lang.StringBuffer objects, initialized with the owner.name property.

Selected Properties List

( path | aggregate ) ( COMMA path | aggregate )*

The properties returned by a SELECT statement can be either a set of properties or an aggregate function. For example, the HQL select max(result.score), min(result.score) from Examresult as result is valid, as is select result from Examresult as result. The statement select result, max(result.score), min(result.score) from Examresult as result, however, is NOT valid.

Aggregate

COUNT OPEN path CLOSE
SUM OPEN path CLOSE
AVG OPEN path CLOSE
MAX OPEN path CLOSE
MIN OPEN path CLOSE
COUNT OPEN STAR CLOSE
COUNT OPEN DISTINCT | ALL path CLOSE

The aggregate functions are used to return statistical data on a set of records rather than the records themselves.

  • avg(path) calculates the mean average value.

  • count(*) returns the number of items in a group, including null values and duplicates.

  • count(ALL path) returns the number of non-null values.

  • count(DISTINCT path) returns the number of unique, non-null values.

  • max(path) finds the maximum value.

  • min(path) finds the minimum value.

  • sum(path) adds the values specified.

Collection Properties

( OPEN query CLOSE ) | ( 'elements'|'indices' OPEN path CLOSE )

Collections have several special properties in addition to the obvious properties that are dependent on the type of collection. All collections can refer to the elements property, which is simply a result of all the possible objects.

For example, select student.examresults.elements.score from com.cascadetg.ch04.Student as student returns the score values from the examresult table as java.lang.Integer, where examresult.studentID is bound to a valid student.id.

As another example, select student.examresults.elements from com.cascadetg.ch04.Student as student returns the set of examresult records, bound as com.cascadetg.ch04.Examresult.

Table 8.3 shows the various collection properties.

Table 8.3. Collection Properties

Property

Meaning

elements

The objects in the collection.

indices

The index values of the collection (only available if the collection is indexed; see Chapter 7).

size

The size of the collection. Useful if you wish to know the number of associated objects without fetching the entire collection.

maxIndex

The maximum index value (only available if the collection is indexed).

minIndex

The minimum index value (only available if the collection is indexed).

maxElement

The element corresponding to the maximum index value (only available if the collection is indexed).

minElement

The element corresponding to the maximum index value (only available if the collection is indexed).

index [...]

Used to find elements in an indexed collection. The expression inside the [ ] tokens is evaluated to determine the resulting elements against the index.


You can extend the path of a collection past the elements property. For example, the statement select student.examresults.elements.score from Student as student returns the scores of the student as java.lang.Integer objects.