All right, we’ve got a handle on what we need to do if we want our albums’ tracks to be kept in the right order. What
about the additional information we’d like to keep, such as the disc on
which the track is found? When we map a collection of associations, we’ve
seen that Hibernate creates a join table in which to store the
relationships between objects. And we’ve just seen how to add an index
column to the
ALBUM_TRACKS table to
maintain an ordering for the collection. Ideally, we’d like to have the
ability to augment that table with more information of our own choosing,
in order to record the other details we’d like to know about album
As it turns out, we can do just that, and in a very straightforward way.
Up to this point we’ve seen two ways of getting tables into our database schema. The first was by explicitly mapping properties of a Java object onto columns of a table. The second was defining a collection (of values or associations), and specifying the table and columns used to manage that collection. As it turns out, there’s nothing that prevents us from using a single table in both ways. Some of its columns can be used directly to map to our own objects’ properties, while the others can manage the mapping of a collection. This lets us achieve our goals of recording the tracks that make up an album in an ordered way, augmented by additional details to support multidisc albums.
This flexibility takes a little ...