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 |
|