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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 3 ■ OBJECT-RELATIONAL MAPPINGThe Address entity uses the mappedBy element on its @OneToOne annotation. Address is called theinverse owner of the relationship because it has a mappedBy element. The mappedBy element indicates thatthe join column (address) is specified at the other end of the relationship. In fact, at the other end, theCustomer entity defines the join column by using the @JoinColumn annotation and renames the foreignkey to address_fk. Customer is the owning side of the relationship, and, as the owner, it is the one todefine the join column mapping. Address is the inverse side where the table of the owning entitycontains the foreign key (the CUSTOMER table is the one <strong>with</strong> the ADDRESS_FK column).There is a mappedBy element on the @OneToOne, @OneToMany, and @ManyToMany annotations, but not onthe @ManyToOne annotation. You cannot have a mappedBy attribute on both sides of a bidirectionalassociation. It would also be incorrect to not have it on either side as the provider would treat it as twoindependent unidirectional relationships. This would imply that each side is the owner and can define ajoin column.■ Note If you are familiar <strong>with</strong> Hibernate, you might think of the JPA mappedBy as the equivalent of the Hibernateinverse attribute.@OneToOne UnidirectionalA one-to-one unidirectional relationship between entities has a reference of cardinality 1, which can bereached in only one direction. Referring to the example of a customer and her address, assume thecustomer has only one address (cardinality 1). It is important to navigate from the customer (the source)toward the address (the target) to know where the customer lives. However, for some reason, in ourmodel, shown in Figure 3-15, you don’t need to be able to navigate in the opposite direction (i.e., youdon’t need to know which customer lives at a given address, for example).Figure 3-15. One customer has one address.In <strong>Java</strong>, this means the Customer will have an Address attribute (see Listings 3-38 and 3-39).97

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

Saved successfully!

Ooh no, something went wrong!