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

Strategies for Getting Started

Throughout this text, I've tried to provide as balanced a set of recommendations as possible. In this section, I offer some general guidelines for those who are still unsure about the proper approach. Consider this section to reflect opinion onlyand keep in mind that since I don't know your application's requirements, these recommendations should be taken with a grain of salt.

Where to Start?

My approach for working with Hibernate depends on whether or not there is an existing database. If you are starting with an existing database, use Middlegen (as described in Chapter 4). If the database schema is extremely stable, you may wish to use Middlegen to generate a first pass at the *.hbm.xml files, and then hand-tune them as needed. If you believe the schema is potentially subject to change, follow the guidelines for Middlegen as described in Chapter 4.

If you are starting from scratch, use the *.hbm.xml mapping as your canonical format, and generate both your Java and database schema based on that (as described in Chapter 2 and Chapter 11). I find that starting from Java (as described in Chapter 3) is most comfortable for developers who are accustomed to using XDoclet as a refuge from the complexity of EJB. I find it much easier to generate persistent objects from my mapping file than to rely on generating get/set methods in bulk and then inspecting the generated *.hbm.xml files.

Start with Many-to-One and One-to-Many

Make sure that you understand the many-to-one and one-to-many relationships before working on the more esoteric types shown in Chapter 7. The vast majority of databases can be modeled relatively conveniently using only these two basic relationships. Class and the more sophisticated relationships are useful tools, but you should feel comfortable with the basic relationships before tackling them.

Profile Database Fetching

When you are first starting out with Hibernate, the relationship between queries and lazy loading of objects can be confusing. You will probably find it especially difficult to understand the implications of the different data-loading strategies. Use the tools shown in Chapter 10 to inspect and time the SQL generated by Hibernate.

Put another way, make sure that you understand how your object graph is going to be traversed. How do you expect to access your data? Make sure that you understand as many as possible of the creation, retrieval, update, and delete (CRUD) operations on your objects.