CHAPTER 7 Advanced entity association mappings When we use the word associations, we always refer to relationships between entities. In the previous chapter, we demonstrated a unidirectional many-to-one association, made it bidirectional, and finally turned it into a parent/children relationship (one-to-many and many-to-one with cascading options). One reason we discuss more advanced entity mappings in a separate chapter is that quite a few of them are considered rare, or at least optional. It s absolutely 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! Of course, efficient and easy access to persistent data, by iterating a collection for example, is one of the reasons why you use full object/relational mapping and not a simple JDBC query service. However, some exotic mapping features should be used with care and even avoided most of the time. We ll point out recommended and optional mapping techniques in this chap ter, as we show you how to map entity associations with all kinds of multiplicity, with and without collections. 7.1 Single-valued entity associations Let s start with one-to-one entity associations. We argued in chapter 4 that the relationships between User and Address (the user has a billingAddress, homeAddress, and shippingAddress) are best represented with a mapping. This is usually the simplest way to represent one-to-one relationships, because the lifecycle is almost always dependent in such a case, it s either an aggregation or a composition in UML. But what if you want a dedicated table for Address, and you map both User and Address as entities? One benefit of this model is the possibility for shared references another entity class (let s say Shipment) can also have a reference to a particular Address instance. If a User has a reference to this instance, as their shippingAddress, the Address instance has to support shared references and needs its own identity. In this case, User and Address classes have a true one-to-one association. Look at the revised class diagram in figure 7.1. The first change is a mapping of the Address class as a stand-alone entity:
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.