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 root entity Item defines the discriminator column once for the entire hierarchy <strong>with</strong>@DiscriminatorColumn. It then changes its own default value to I <strong>with</strong> the @DiscriminatorValueannotation. Child entities have to redefine their own discriminator value only.Listing 3-62. Book Redefines the Discriminator Value to B@Entity@DiscriminatorValue("B")public class Book extends Item {private String isbn;private String publisher;private Integer nbOfPage;private Boolean illustrations;}// Constructors, getters, settersListing 3-63. CD Redefines the Discriminator Value to C@Entity@DiscriminatorValue("C")public class CD extends Item {private String musicCompany;private Integer numberOfCDs;private Float totalDuration;private String gender;}// Constructors, getters, settersThe result is shown in Figure 3-25. The discriminator column and its values are different from thoseshown earlier in Figure 3-24.Figure 3-25. The ITEM table <strong>with</strong> a different discriminator name and valuesThe single-table-per-class strategy is the default, is the easiest to understand, and works well whenthe hierarchy is relatively simple and stable. However, it has some drawbacks; adding new entities to thehierarchy, or adding attributes to existing entities, involves adding new columns to the table, migratingdata, and changing indexes. This strategy also requires the columns of the child entities to be nullable. If114

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

Saved successfully!

Ooh no, something went wrong!