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

Chapter 6. Persistent Objects

Once you've described your persistence with Hibernate *.hbm.xml files and the corresponding Java sources, the basic mechanism for creating, updating, finding, and deleting objects is the Session class. This will be discussed in depth in the first part of the chapter.

Hibernate makes an important distinction between transient objects and persistent objects. A transient object is not associated with a particular session. A persistent object is associated with a session. This can be a subtle (and confusing) distinction when working with Hibernate, but it's important to keep in mind that the objects you are working with may not represent the correct state of affairs as represented in the database outside the context of a session.

The notion of persistent and transient objects is intertwined with the notion of object identity. The review of the session interface will be followed by an in-depth look at the various notions of identity. As long as you stick to a single thread per net.sf.hibernate.Session object, Hibernate will largely take care of identity automatically, but it can be confusing if you encounter problems related to identity (in particular, if you make use of composite identifiers).

Finally, the life-cycle methods provided by Hibernate will be reviewed. These methods allow the receipt of events at various points in an object's existence. Sample code for a simple object is shown, and can be used as the base for your own laboratory experiments with object identity.