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 MAPPINGAs discussed previously, the @ElementCollection annotation is used to indicate the objects in themap that are stored in a collection table. The @CollectionTable annotation changes the default name ofthe collection table to TRACK.The difference <strong>with</strong> collections is the introduction of a new annotation: @MapKeyColumn. Thisannotation is used to specify the mapping for the key column of the map. If it is not specified, thecolumn is named <strong>with</strong> the concatenation of the name of the referencing relationship attribute and _KEY.Listing 3-28 shows the annotation renamed to POSITION from the default (TRACK_KEY) to be clearer.The @Column annotation indicates that the column containing the map value should be renamedTITLE. The result is shown in the Figure 3-4.Figure 3-4. Relationship between the CD and the TRACK tablesMapping <strong>with</strong> XMLNow that you are more familiar <strong>with</strong> elementary mapping using annotations, let’s take a look at XMLmapping. If you have used an object-relational framework such as Hibernate, you will be familiar <strong>with</strong>how to map your entities in a separate XML deployment descriptors file. Since the beginning of thischapter, you haven’t seen a single line of XML, just annotations. JPA also offers, as an option, an XMLsyntax to map entities. I will not go into too much detail about the XML mapping, as I’ve decided tofocus on annotations (because they are easier to use in a book and most developers choose them overXML mapping). Keep in mind that every single annotation you see in this chapter has an XMLequivalent, and this section would be huge if I covered them all. I encourage you to check Chapter 12(XML Object/Relational Mapping Descriptor) of the JPA 2.0 specification, which covers all the XML tagsin more detail.XML deployment descriptors are an alternative to using annotations. However, although eachannotation has an equivalent XML tag and vice versa, there is a difference in that the XML overridesannotations. If you annotate an attribute or an entity <strong>with</strong> a certain value, and at the same time youdeploy an XML descriptor <strong>with</strong> a different value, the XML will take precedence.The question is, when should you use annotations over XML and why? First of all, it’s a matter oftaste, as the behavior of both is exactly the same. When the metadata is really coupled to the code (forexample, a primary key), it does make sense to use annotations, since the metadata is just anotheraspect of the program. Other kinds of metadata, such as the column length or other schema details, canbe changed depending on the deployment environment (e.g., the database schema is different in thedevelopment, test, or production environment). A similar situation arises when a JPA-based productneeds to support several different database vendors. Certain id generation, column options, etc. mayneed to be adjusted depending on the database type in use. This may be better expressed in externalXML deployment descriptors (one per environment) so the code doesn’t have to be modified.Let’s again turn to the Book entity example. This time imagine you have two environments, and youwant to map the Book entity to the BOOK table in the development environment and to theBOOK_XML_MAPPING table in the test environment. The class will only be annotated <strong>with</strong> @Entity (seeListing 3-29) and not include information about the table it should be mapped to (i.e., it will have no84

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

Saved successfully!

Ooh no, something went wrong!