You may have noticed that we never defined a persistence mapping
class in the first part of this chapter. That’s because our enumerated
type is a value that gets persisted as
part of one or more entities, rather than being an entity unto
In that light, it’s not surprising that we’ve not yet done any mapping. That happens when it’s time to actually use the persistent enumeration—which is to say, now.
Recall that we wanted to keep track of the source media for the
music tracks in our jukebox system. That means we want to use the
SourceMedia enumeration in our
Track mapping. We can simply add a new
property tag to the
class definition in Track.hbm.xml, as shown in Example 6-3.
Example 6-3. Adding the sourceMedia property to the track mapping document
... <property name="volume" type="short"> <meta attribute="field-description">How loud to play the track</meta> </property>
<property name="sourceMedia" type="com.oreilly.hh.SourceMediaType">
<meta attribute="field-description">Media on which track was obtained</meta>
</property><set name="comments" table="TRACK_COMMENTS"> ...
Notice that we’ve told Hibernate that the type of this property is
UserType implementation, not the raw
enumeration type that it is responsible for helping persist. Because the
type of our
sourceMedia property names
a class that implements the
UserType interface, Hibernate knows to delegate ...