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

Java Collection Relationships

A collection is a group of objects related to another group of objects by aggregation. For example, a weblog has a collection of posts. Depending on the type of collection, this may mean a one-to-one, many-to-one, one-to-many, or many-to-many relationship. In other words, to represent a Java collection, you will first need to choose the underlying database relationship (as described above).

Most developers assume that an ordinary foreign-key-based, one-to-many relationship is best modeled with a java.util.List. However, the precise index of a relationship is actually not included as a foreign key. For example, consider a weblog with a relationship to a number of posts. While you may retrieve the posts in order (for example, sorted by date), the precise index of the post as part of the relationship is not part of the database record. Instead, the result set (and ordering) is an artifact of the runtime results as retrieved by the query.

Array: An ordinary, pure Java array. Keep in mind that an array is a primitive, and therefore must be loaded entirely by the owning class (i.e., it cannot be specified as lazy; see tag class, attribute lazy in Chapter 5 for more information).

Bag: An unordered collection that may contain duplicate elements.

List: An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. A sorted map will maintain the keys in order.

Primitive Array: An array of Java primitive types. Like an array, loaded entirely by the owning class and cannot be lazily loaded.

Set: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. A sorted set will maintain the elements in order.

Several of these collection types enforce rules regarding duplicates. The rules for identity (used to handle duplicates) are described in more detail in Chapter 6.

Table 7.1 compares the different features of the collection types. The implementation column is provided for reference if you are unsure which implementation to use when creating a new collection of the appropriate type; technically, you can use any implementation of the interface shown. You should only work with these collections using the interface shown, and never rely on the underlying implementation, because Hibernate will sometimes change the underlying implementation class (for example, as part of the ability of Hibernate to manage lazy-loaded relationships).

Table 7.1. Collection Features
 

Interface

Implementation

Duplicates

Keys

Indexed

Lazy

Ordered

Array

Object[]

Object[]

Yes

No

Yes

No

Yes

Bag

java.util.List

java.util.ArrayList

Yes

No

No

Yes

No

List

java.util.List

java.util.ArrayList

Yes

No

Yes

Yes

Yes

Map

java.util.Map

java.util.HashMap
java.util.SortedMap

No

Yes

No

Yes

Optional

Primitive Array

primitive[]

n/a

Yes

No

Yes

No

Yes

Set

java.util.Set

java.util.HashSet
java.util.SortedSet

No

No

No

No

Optional


For more information on the various built-in JDK 1.4 collections and alternative interfaces, see http://java.sun.com/j2se/1.4.2/docs/guide/collections/reference.html.

Interface: The expected Java interface for this collection type.

Implementation: A suggested Java implementation.

Duplicates: Are duplicate elements allowed?

Keys: Are elements accessible by a key value (e.g. ,java.util.Hashtable)?

Indexed: Does a column in the database maintain the order of the elements?

Lazy: Can the parent object be loaded without loading the collection data?

Ordered: Are the results returned as a sorted collection?

The feature set shown in Table 7.1 has both functional implications and performance implications. Table 7.2 shows a rough guide to the relative performance of the different collection types.

Table 7.2. Relative Collection Performance
 

Keys

Indexed

Lazy

Insert

Update

Delete

Array

No

Yes

No

Medium

Medium

Medium

Bag

No

No

Yes

High

Poor

Poor

List

No

Yes

Yes

Medium

High

Medium

Map

Yes

No

Yes

Medium

High

Medium

Primitive Array

No

Yes

No

Medium

Medium

Medium

Set

No

No

No

Medium

High

Medium