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 MAPPINGListing 3-34. The Customer Entity Embedding an Address@Entitypublic class Customer {@Id @GeneratedValueprivate Long id;private String firstName;private String lastName;private String email;private String phoneNumber;@Embeddedprivate Address address;}// Constructors, getters, settersEach attribute of Address is mapped to the table of the owning entity Customer. There will only beone table <strong>with</strong> the structure defined in Listing 3-35. As you’ll see later in the section “OverridingAttributes,” entities can override the attributes of embeddables (using the @AttributeOverridesannotation).Listing 3-35. Structure of the CUSTOMER Table <strong>with</strong> All the Address Attributescreate table CUSTOMER (ID BIGINT not null,LASTNAME VARCHAR(255),PHONENUMBER VARCHAR(255),EMAIL VARCHAR(255),FIRSTNAME VARCHAR(255),STR<strong>EE</strong>T2 VARCHAR(255),STR<strong>EE</strong>T1 VARCHAR(255),ZIPCODE VARCHAR(255),STATE VARCHAR(255),COUNTRY VARCHAR(255),CITY VARCHAR(255),primary key (ID));■ Note In the previous sections I showed you how to map collections and maps of basic data types. In JPA 2.0,the same is possible <strong>with</strong> embeddables. You can map collections of embeddables as well as maps ofembeddables (the embeddable can be either the key or the value of the map).88

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

Saved successfully!

Ooh no, something went wrong!