The javax.persistence.EntityManager is the central service for all persistence actions. Entity beans are plain Java objects that interact with EntityManager to make them persistent. EntityManager has the following role:
- manages the object-relational mapping between a fixed set of entity classes and the underlying data source.
- provides APIs for creating queries, finding objects, synchronizing objects and inserting objects into the database.
- provides caching and manage the interaction between an entity and transactional services.
- tracks state changes to the entity bean.
EntityManager manages persistence context, a set of entity bean instances. When the persistence context is closed, all managed entity objects become detached and are unmanaged.
I will be writing a series of articles related to EJB 3.0 in preparation for my SCBCD exam. The main objective is to keep updating the site while studying intensively for one month. The notes are prepared by reading through Enterprise JavaBeans 3.0, Fifth Edition by Bill Burke and Richard Monson-Haefel.
Entity Bean
In EJB 3.0, persistence has been spun off from EJB to its own specification called Java Persistence API. The API defines a way to map plain old Java objects (POJOs) to a database. These plain old Java objects are called entity beans.
The Java Persistence API also defines EJB QL that is tailored to work with Java objects rather than a raw relational schema. Entity beans are different from EJB session beans in that they are POJOs. They do not have a remote or local interface and can be accessed only as POJOs.
In order to “make” a POJO class as an entity bean class, you need to tag the class with @javax.persistence.Entity annotation and have at least one field or getter method that is designated as the primary key using @javax.persistence.Id annotation. All access to an entity goes through EntityManager persistence service that provides query API and life cycle methods.
Continue reading ‘SCBCD Notes: EJB & Java Persistence Architecture’