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 MAPPINGstructures, as they are <strong>with</strong> objects. Sometimes mapping <strong>Java</strong> objects to the underlying database can beeasy, and the default rules can be applied. At other times, these rules do not meet your needs, and youmust customize the mapping. Elementary mapping annotations focus on customizing the table, theprimary key, and the columns, and they let you modify certain naming conventions or typing (not-nullcolumn, length, etc.).TablesRules for configuration-by-exception mapping state that the entity and the table name are the same (aBook entity is mapped to a BOOK table, an AncientBook entity is mapped to an ANCIENTBOOK table, and soon). This might suit you in most cases, but you may want to map your data to a different table, or evenmap a single entity to several tables.@TableThe @javax.persistence.Table annotation makes it possible to change the default values related to thetable. For example, you can specify the name of the table in which the data will be stored, the catalog,and the database schema. If this annotation is omitted, the name of the table will be the name of theentity. If you want to change the name to T_BOOK instead of BOOK, you would do as shown in Listing 3-3.Listing 3-3. The Book Entity Being Mapped to a T_BOOK Table@Entity@Table(name = "t_book")public class Book {@Idprivate Long id;private String title;private Float price;private String description;private String isbn;private Integer nbOfPage;private Boolean illustrations;public Book() {}}// Getters, setters■ Note In the @Table annotation, I include a lowercase table name (t_book). By default, most databases will mapthe entity to an uppercase table name (and that’s the case <strong>with</strong> Derby) except if you configure them to honor case.65

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

Saved successfully!

Ooh no, something went wrong!