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 MAPPINGAn important notion to always have in mind is that the XML takes precedence on annotations. Evenif the description attribute is annotated by @Column(length = 500), the length of the column used is theone in the book_mapping.xml file, which is 2000. This can be confusing as you look at the code and see 500and then check the DDL and see 2000; always remember to check the XML deployment descriptor.A result of merging the XML metadata and the annotations metadata is that the Book entity will getmapped to the BOOK_XML_MAPPING table structure defined in Listing 3-31. If you want to completely ignorethe annotations and define your mapping <strong>with</strong> XML only, you can add the tag to the book_mapping.xml file (in this case, all the annotations will be ignored even if theXML does not contain an override).Listing 3-31. BOOK_XML_MAPPING Table Structurecreate table BOOK_XML_MAPPING (ID BIGINT not null,BOOK_TITLE VARCHAR(255) not null,DESCRIPTION VARCHAR(2000),NB_OF_PAGE INTEGER not null,PRICE DOUBLE(52, 0),ISBN VARCHAR(255),ILLUSTRATIONS SMALLINT,primary key (ID));There is only one piece of information missing to make this work. In your persistence.xml file, youneed to reference the book_mapping.xml file, and for this you have to use the tag. Thepersistence.xml defines the entity persistence context and the database it should be mapped to. It is thecentral piece of information that the persistence provider needs to reference external XML mapping.Deploy the Book entity <strong>with</strong> both XML files in the META-INF directory, and you are done (see Listing 3-32).Listing 3-32. A persistence.xml File Referring to an External Mapping Fileorg.eclipse.persistence.jpa.PersistenceProvidercom.apress.javaee6.chapter05.Book86

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

Saved successfully!

Ooh no, something went wrong!