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 4 ■ MANAGING PERSISTENT OBJECTSThe chapter04PU persistence unit defines a JDBC connection for the chapter04DB Derby database. Itconnects <strong>with</strong> a user (APP) and a password (APP) at a given URL. The tag tells the persistenceprovider to manage the Book class (there are other tags to implicitly or explicitly denote managedpersistence classes such as , , or ).To make the code work, in addition to a Derby database running on port 1527, both a Book and Mainclass need to be compiled and deployed <strong>with</strong> this META-INF/persistence.xml file. By enabling logging,you might see some traces of SQL statements, but your code manipulates objects, in an object-friendlyway, <strong>with</strong> no SQL statement or JDBC direct calls using the EntityManager API.Entity ManagerThe entity manager is a central piece in JPA. It manages the state and life cycle of entities as well asquerying entities <strong>with</strong>in a persistence context. The entity manager is responsible for creating andremoving persistent entity instances and finding entities by their primary key. It can lock entities forprotecting against concurrent access by using optimistic or pessimistic locking and can use JPQL queriesto retrieve entities following certain criteria.When an entity manager obtains a reference to an entity, it is said to be managed. Until that point,the entity is seen as a regular POJO (i.e., detached). The strength of JPA is that entities can be used asregular objects by different layers of an application and become managed by the entity manager whenyou need to load or insert data into the database. When an entity is managed, you can carry outpersistence operations, and the entity manager will automatically synchronize the state of the entity<strong>with</strong> the database. When the entity is detached (i.e., not managed), it returns to a simple POJO and canthen be used by other layers (e.g., a JSF presentation layer) <strong>with</strong>out synchronizing its state <strong>with</strong> thedatabase.With the entity manager, the real work of persistence starts. EntityManager is an interfaceimplemented by a persistence provider that will generate and execute SQL statements. Thejavax.persistence.EntityManager interface provides the API shown in Listing 4-4 to manipulate entities.Listing 4-4. EntityManager APIpublic interface EntityManager {EntityTransaction getTransaction();EntityManagerFactory getEntityManagerFactory();void close();boolean isOpen();void persist(Object entity); T merge(T entity);void remove(Object entity); T find(Class entityClass, Object primaryKey); T find(Class entityClass, Object primaryKey, Map properties); T find(Class entityClass, Object primaryKey, LockModeType lockMode); T find(Class entityClass, Object primaryKey,LockModeType lockMode, Map properties); T getReference(Class entityClass, Object primaryKey);void flush();void setFlushMode(FlushModeType flushMode);127

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

Saved successfully!

Ooh no, something went wrong!