9 Advanced entity association mappings


This chapter covers

  • Applying mapping through one-to-one entity associations
  • Using one-to-many mapping options
  • Creating many-to-many and ternary entity relationships

In the previous chapter, we demonstrated a unidirectional many-to-one association, made it bidirectional, and finally enabled transitive state changes with cascading options. One reason we discuss more advanced entity mappings in a separate chapter is that we consider quite a few of them rare or at least optional. It’s possible to only use component mappings and many-to-one (occasionally one-to-one) entity associations. You can write a sophisticated application without ever mapping a collection! We’ve demonstrated the particular benefits to gain from collection mappings in the previous chapter; the rules for when a collection mapping is appropriate also apply to all examples in this chapter. Always make sure you actually need a collection before attempting a complex collection mapping.

Let’s start with mappings that don’t involve collections: one-to-one entity associations.

9.1 One-to-one associations

9.1.1 Sharing a primary key

9.1.2 The foreign primary key generator

9.1.3 Using a foreign key join column

9.1.4 Using a join table

9.2 One-to-many associations

9.2.1 Considering one-to-many bags

9.2.2 Unidirectional and bidirectional list mappings

9.2.3 Optional one-to-many with a join table

9.2.4 One-to-many association in an embeddable class

9.3 Many-to-many and ternary associations

9.3.1 Unidirectional and bidirectional many-to-many associations

9.3.2 Many-to-many with an intermediate entity

9.3.3 Ternary associations with components

9.4 Entity associations with Maps

9.4.1 One-to-many with a property key

9.4.2 Key/Value ternary relationship

9.5 Summary