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

Native SQL Queries

In addition to HQL and Criteria, Hibernate allows you to retrieve data from the database using native SQL. Describing SQL would go beyond the scope of this text, but it should be noted here that Hibernate requires aliases to be used when working with native SQL. The aliases are used to bind the SQL to the returned objects. Listing 8.7 shows an example of the execution of a native SQL statement binding to the return objects.

Listing 8.7. Executing Native SQL
session.createSQLQuery("SELECT {student.*} FROM student AS {student}
WHERE ID<10", "student", Student.class).list();

If you simply wish to execute raw SQL against the database (and not retrieve a set of objects bound to a query), you will probably need to query the session for the underlying JDBC connection. An example of the use of raw SQL execution from a Hibernate session can be found in Listing 2.7 (the setInnoDB() method).

Keep in mind that SQL statements mixed with other statements may not execute in the order desired unless you use the Session.flush() method. For more on this, see Chapter 9.

The full scope and theory of SQL is beyond the scope of this book (especially considering the wide variety of proprietary extensions). Consult your database documentation or another text on SQL for more information on native SQL queries.