O'Reilly logo

Harnessing Hibernate by James Elliott, Ryan Fowler, Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a Composite User Type

Recall that in our Track object we have a property that determines our preferred playback volume for the track. Suppose we’d like the jukebox system to be able to adjust the balance of tracks for playback, rather than just their volume. To accomplish this we’d need to store separate volumes for the left and right channels. The quick solution would be to edit the Track mapping to store these as separate mapped properties.

If we’re serious about object-oriented architecture, we might want to encapsulate these two values into a StereoVolume class. This class could then simply be mapped as a composite-element, as the AlbumTrack component was in Example 5-4. This is still fairly straightforward.

There is a drawback, however, to this simple approach. It’s likely we will discover other places in our system where we want to represent StereoVolume values. If we build a playlist mechanism that can override a track’s default playback options, and also want to be able to assign volume control to entire albums, suddenly we have to recreate the composite mapping in several places, and we might not do it consistently everywhere (this is more likely to be an issue with a more complex compound type, but you get the idea). The Hibernate reference documentation says that it’s a good practice to use a composite user type in situations like this, and I agree.

How do I do that?

Let’s start by defining the StereoVolume class. There’s no reason for this to be an entity (to have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required