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 MAPPINGListing 3-70. The BOOK Table Has No Attributes from Itemcreate table BOOK (ID BIGINT not null,ILLUSTRATIONS SMALLINT,ISBN VARCHAR(255),NBOFPAGE INTEGER,PUBLISHER VARCHAR(255),primary key (ID));Mapped SuperclassJPA defines a special kind of class, called a mapped superclass, to share state and behavior, as well asmapping information entities inherit from. However, mapped superclasses are not entities. They are notmanaged by the persistence provider, do not have any table to be mapped to, and cannot be queried orbe part of a relationship, but they may provide persistent properties to any entities that extend it. Theyare similar to embeddable classes except they can be used <strong>with</strong> inheritance. A class is indicated as beinga mapped superclass by annotating it <strong>with</strong> the @MappedSuperclass annotation.Using the root class, Item is annotated <strong>with</strong> @MappedSuperclass, not @Entity, as shown inListing 3-71. It defines an inheritance strategy (JOINED) and annotates some of its attributes <strong>with</strong> @Column,but because mapped superclasses are not mapped to tables, the @Table annotation is not permitted.Listing 3-71. Item Is a Mapped Superclass@MappedSuperclass@Inheritance(strategy = InheritanceType.JOINED)public class Item {@Id @GeneratedValueprotected Long id;@Column(length = 50, nullable = false)protected String title;protected Float price;@Column(length = 2000)protected String description;}// Constructors, getters, settersAs you can see in Listing 3-71, the title and description attributes are annotated <strong>with</strong> @Column.Listing 3-72 shows the Book entity extending Item.120

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

Saved successfully!

Ooh no, something went wrong!