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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ OBJECT-RELATIONAL MAPPINGpublic void setFirstName(String firstName) {this.firstName = firstName;}@Column(name = "last_name", nullable = false, length = 50)public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}}@Column(name = "phone_number", length = 15)public String getPhoneNumber() {return phoneNumber;}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}In terms of mapping, the two entities in Listings 3-24 and 3-25 are completely identical because theattribute names happen to be the same as the getter names. But instead of using the default access type,you can explicitly specify the type by means of the @javax.persistence.Access annotation.This annotation takes two possible values, FIELD or PROPERTY, and can be used on the entity itselfand/or on each attribute or getter. For example, when an @Access(AccessType.FIELD) is applied to theentity, only mapping annotations placed on the attributes will be taken into account by the persistenceprovider. It is then possible to selectively designate individual getters for property access <strong>with</strong>@Access(AccessType.PROPERTY).Explicit access types can be very useful (for example, <strong>with</strong> embeddables and inheritance) but mixingthem often results in errors. Listing 3-26 shows an example of what might happen when you mix accesstypes.Listing 3-26. The Customer Entity That Explicitly Mixes Access Types@Entity@Access(AccessType.FIELD)public class Customer {@Id @GeneratedValueprivate Long id;@Column(name = "first_name", nullable = false, length = 50)807

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

Saved successfully!

Ooh no, something went wrong!