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 2 ■ JAVA PERSISTENC<strong>EE</strong>clipseLink’s origins stem from the Oracle TopLink product given to the Eclipse Foundation in 2006.EclipseLink is the JPA reference implementation and is the persistence framework used in this book. It isalso referred to as the persistence provider, or simply the provider.Understanding EntitiesWhen talking about mapping objects to a relational database, persisting objects, or querying objects, theterm “entity” should be used rather than “objects.” Objects are instances that just live in memory.Entities are objects that live shortly in memory and persistently in a database. They have the ability to bemapped to a database; they can be concrete or abstract; and they support inheritance, relationships, andso on. These entities, once mapped, can be managed by JPA. You can persist an entity in the database,remove it, and query it using a query language (<strong>Java</strong> Persistence Query Language, or JPQL). ORM lets youmanipulate entities, while under the covers the database is being accessed. And, as you will see, an entityfollows a defined life cycle. With callback methods and listeners, JPA lets you hook some business codeto life-cycle events.Object-Relational MappingThe principle of ORM is to delegate to external tools or frameworks (in our case, JPA) the task of creatinga correspondence between objects and tables. The world of classes, objects, and attributes can then bemapped to relational databases made of tables containing rows and columns. Mapping gives an objectorientedview to developers who can transparently use entities instead of tables. And how does JPA mapobjects to a database? Through metadata.Associated <strong>with</strong> every entity is metadata that describes the mapping. This metadata enables thepersistence provider to recognize an entity and to interpret the mapping. This metadata can be writtenin two different formats:• Annotations: The code of the entity is directly annotated <strong>with</strong> all sorts ofannotations that are described in the javax.persistence package.• XML descriptors: Instead of (or in addition to) annotations, you can use XMLdescriptors. The mapping is defined in an external XML file that will be deployed<strong>with</strong> the entities. This can be very useful when database configuration changesdepending on the environment, for example.To make the mapping easier, JPA (like many other <strong>Java</strong> <strong>EE</strong> 6 specifications) uses the concept ofconfiguration by exception (sometimes referred to as programming by exception). The idea is that JPA hascertain default mapping rules (e.g., the table name is the same as the entity name). If you are happy <strong>with</strong>them, you don’t need to use extra metadata (no annotation or XML is needed), but, if you don’t want theprovider to apply the default rules, you can customize the mapping to your own needs using metadata.In other words, having to supply a configuration is the exception to the rule.So let’s see how this applies to an entity. Listing 2-1 shows a Book entity <strong>with</strong> some attributes. As youcan see, some of them are annotated (id, title, and description), and some are not.46

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

Saved successfully!

Ooh no, something went wrong!