12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ OBJECT-RELATIONAL MAPPING@OneToMany, @ManyToOne, or @ManyToMany annotations. Each annotation can be used in a unidirectional orbidirectional way. Table 3-1 shows all the possible combinations between cardinalities and directions.Table 3-1. All Possible Cardinality-Direction CombinationsCardinalityOne-to-oneOne-to-oneOne-to-manyMany-to-one/one-to-manyMany-to-oneMany-to-manyMany-to-manyDirectionUnidirectionalBidirectionalUnidirectionalBidirectionalUnidirectionalUnidirectionalBidirectionalYou will see that unidirectional and bidirectional are repetitive concepts that apply in the same wayto all cardinalities. Next, you will see the difference between unidirectional and bidirectionalrelationships and then implement some of these combinations. I will not go through the completecatalog of the combinations but just focus on a subset. Explaining all the combinations would getrepetitive. The important point is that you understand how to map cardinality and direction inrelationships.Unidirectional and BidirectionalIn an object-modeling point of view, direction between classes is natural. In a unidirectional association,object A points only to object B; in a bidirectional association, both objects refer to each other. However,some work is necessary when mapping a bidirectional relationship to a relational database, as isillustrated by the following example involving a customer who has a home address.In a unidirectional relationship, a Customer entity has an attribute of type Address (see Figure 3-11).The relationship is one-way, navigating from one side to the other. Customer is said to be the owner ofthe relationship. In terms of the database, this means the CUSTOMER table will have a foreign key (joincolumn) pointing to ADDRESS, and, when you own a relationship, you are able to customize the mappingof this relationship. For example, if you need to change the name of the foreign key, the mapping will bedone in the Customer entity (i.e., the owner).94

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!