Chapter 5. Richer Associations

Yes, wealthy friends would be nice. But we can’t propose an easy way to get any, so instead let’s look at relationships between objects that carry more information than simple grouping. In this chapter we’ll look at the tracks that make up an album. We put that off in Chapter 4 because organizing an album involves more than simply grouping some tracks; you also need to know the order in which the tracks occur, as well as things like which disc they’re on, if you want to support multidisc albums. That goes beyond what you can achieve with an automatically generated join table, so we’ll design our own AlbumTrack object and table, and let albums link to these.

Eager and Lazy Associations

First rich, then eager and lazy? It sure sounds like we’re anthropomorphizing our data model. But this really is an object/relational mapping topic of some importance. As your data model grows, adding associations between objects and tables, your program gains power, which is great. But you often end up with a large fraction of your objects somehow linked to each other. So, what happens when you load one of the objects that is part of a huge interrelated cluster? Since, as you’ve seen, you can move from one object to its associated objects just by traversing properties, it seems you’d have to load all the associated objects when you load any of them. For small databases, this is fine (and indeed, the HSQLDB databases we’ve been playing with in the examples exist entirely ...

Get Harnessing Hibernate now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.