Thursday, April 24, 2014

Learning Java - What's BMP, CMP, and JPA?

Today, I am learning the difference among Java's ways to interact with a database or storage device.

JPA: Java Persistence Architecture API

The JPA is a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database. JPA was defined as part of the EJB 3.0 specification as a replacement for the EJB 2 CMP Entity Beans specification. JPA is now considered the standard industry approach for Object to Relational Mapping (ORM) in the Java industry.
JPA allows POJO (Plain Old Java Objects) to be easily persisted without requiring the classes to implement any interfaces or methods as the EJB 2 CMP specification required. JPA allows an object's object-relational mappings to be defined through standard annotations or XML defining how the Java class maps to a relational database table. JPA also defines a runtime EntityManager API for processing queries and transaction on the objects against the database. JPA defines an object-level query language, JPQL, to allow querying of the objects from the database.

Why JPA replaced BMP/CMP?

Entity Beans calls for to much complicated code and heavy resource footprint, and they could be used only in Java EE application servers because of interconnections and dependencies in the source code between beans and DAO objects (or persistent framework).

Reference(s): 

http://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F
http://en.wikipedia.org/wiki/Java_Persistence_API
http://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.html

CMP: Container-Managed Persistence

A CMP bean is an entity bean whose state is synchronized with the database automatically.
In other words, the bean developer doesn't need to write any explicit database calls into the bean code; the container will automatically synchronize the persistent fields with the database as dictated by the deployer at deployment time.
When a CMP bean is deployed, the deployer uses the EJB tools provided by the vendor to map the persistent fields in the bean to the database. The persistence fields will be a subset of the instance fields, called container-managed fields, as identified by the bean developer in the deployment descriptor.

Reference(s): 

http://www.jguru.com/faq/view.jsp?EID=1087

BMP: Bean-Managed Persistence

A BMP bean is an entity that synchronizes its state with the database manually.
In other words, the bean developer must code explicit database calls into the bean itself. BMP provides the bean developer with more flexibility in how the bean reads and writes its data than a container-managed persistence (CMP) bean. CMP bean is limited to the mapping facilities provided by the EJB vendor, BMP beans are only limited by skill of the bean developer.
The ability to code an entity bean's persistence logic explicitly is important when the EJB container's CMP features are insufficient to meet the needs of the entity bean. Entity beans that need to synchronize their state with several data sources are excellent candidates for BMP. So are beans that need to access data sources (possibly legacy systems) that are not supported by CMP. In addition, BMP bean are often employed when a vendor's CMP facilities are not sophisticated enough to handle complex Object-to-Relational mapping.

Reference(s): 

http://www.jguru.com/faq/view.jsp?EID=1090

Remember

  • JPA replaces CMP & BMP as Java industry standard's Object-to-Relational Mapping (ORM).
  • CMP bean is limited to the mapping facilities provided by the EJB vendor; BMP beans are only limited by skill of the bean developer.

Additional References:




1 comment: