Hibernate defines three basic kinds of collections:
- collections of values
- one-to-many associations
- many-to-many associations
Lists, maps, idbags and sets are the most efficient collections to update
This classification distinguishes the various table and foreign key relationships but does not tell us quite everything we need to know about the relational model. To fully understand the relational structure and performance characteristics, we must also consider the structure of the primary key that is used by Hibernate to update or delete collection rows. This suggests the following classification:
- indexed collections (Maps, Lists, Arrays)
- sets
- bags
All indexed collections (maps, lists, and arrays) have a primary key consisting of the <key> and <index> columns. In this case, collection updates are extremely efficient. The primary key can be efficiently indexed and a particular row can be efficiently located when Hibernate tries to update or delete it.
Sets have a primary key consisting of <key> and element columns. This can be less efficient for some types of collection element, particularly composite elements or large text or binary fields, as the database may not be able to index a complex primary key as efficiently. However, for one-tomany or many-to-many associations, particularly in the case of synthetic identifiers, it is likely to be just as efficient. If you want SchemaExport to actually create the primary key of a <set>, you must declare all columns as not-null="true".
<idbag> mappings define a surrogate key, so they are efficient to update. In fact, they are the best case.
Bags are the worst case since they permit duplicate element values and, as they have no index column, no primary key can be defined. Hibernate has no way of distinguishing between duplicate rows. Hibernate resolves this problem by completely removing in a single DELETE and recreating the collection whenever it changes. This can be inefficient.
For a one-to-many association, the "primary key" may not be the physical primary key of the
database table. Even in this case, the above classification is still useful. It reflects how Hibernate "locates" individual rows of the collection.
Lists, maps, idbags and sets are the most efficient collections to update
Bags and lists are the most efficient inverse collections
one more advantage that indexed collections have over sets for many-to-many
associations or collections of values. Because of the structure of a Set, Hibernate does not UPDATE a row when an element is "changed". Changes to a Set always work via INSERT and DELETE of individual rows. Once again, this consideration does not apply to one-to-many associations.
One shot delete -One-shot-delete does not apply to collections mapped inverse="true".
Monitoring a SessionFactory
You can access SessionFactory metrics in two ways. Your first option is to callsessionFactory.getStatistics() and read or display the Statistics yourself.