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 MAPPINGFigure 3-1. Data synchronization between the entity and the tableConfiguration by Exception<strong>Java</strong> <strong>EE</strong> 5 introduced the idea of configuration by exception (sometimes referred to as programming byexception or convention over configuration). This means, unless specified differently, the container orprovider should apply the default rules. In other words, having to supply a configuration is the exceptionto the rule. This allows you to write the minimum amount of code to get your application running,relying on the container and provider defaults.Let’s return to the previous code example (see Listing 3-1). Without any annotation, the Book entitywould be treated just like a POJO and not be persisted. That is the rule: if no special configuration isgiven, the default should be applied, and the default for the persistence provider is that the Book classhas no database representation. But because you need to change this default behavior, you annotate theclass <strong>with</strong> @Entity. It is the same for the identifier. You need a way to tell the persistence provider thatthe id attribute has to be mapped to a primary key, so you annotate it <strong>with</strong> @Id. This type of decisioncharacterizes the configuration-by-exception approach, in which annotations are not required for themore common cases and are only used when an override is needed. This means that, for all the otherattributes, the following default mapping rules will apply:• The entity name is mapped to a relational table name (e.g., the Book entity ismapped to a BOOK table). If you want to map it to another table, you will need touse the @Table annotation, as you’ll see later in the “Elementary Mapping” section.63

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

Saved successfully!

Ooh no, something went wrong!