Java Reference
In-Depth Information
<set name="adverts"
table="user_advert_link"
cascade="save-update">
<key column="userid"/>
<many-to-many
class="com.hibernatebook.xmlmapping.Advert"
column="advertid"/>
</set>
Hibernate's use of collections tends to expose the lazy loading issues more than most
other mappings. If you enable lazy loading, the collection that you retrieve from the session
will be a proxy implementing the relevant collection interface (in our example, Set ), rather
than one of the usual Java concrete collection implementations.
This allows Hibernate to retrieve the contents of the collection only as they are required
by the user. If you load an entity, consult a single item from the collection, and then discard it,
often only a handful of SQL operations will be required. If the collection in question repre-
sents hundreds of entity instances, the performance advantages of lazy loading (compared
with the massive task of reading in all of the entities concerned) are massive.
However, you will need to ensure that you do not try to access the contents of a lazily
loaded collection at a time when it is no longer associated with the session, unless you can be
certain that the contents of the collection that you are accessing have already been loaded.
Mapping Inheritance Relationships
Figure 7-5 shows a simple class hierarchy. The superclass is Advert , and there are two classes
derived from this: a Personal class to represent personal advertisements and a Property class
to represent property advertisements.
Figure 7-5. A simple inheritance hierarchy
Search WWH ::




Custom Search