Getting Started with Enterprise JavaBeans™

G

Entity Beans

Tutorial Home Section Home Previous Section Next

Get

Get

Entity Beans with Container-Managed Persistence

     Put another way, the primary difference between BMP and CMP mentioned in Entity Beans with Bean-Managed Persistence is that with CMP, the container handles the datastore access, insert, update, and deletion routines. Another big difference in version 2.0 of the EJB specification is that an entity bean class with CMP must be declared abstract.

     Let's talk about that for a moment. Abstract classes are normally used when there is a partial implementation and the class wants to mandate the other methods used without specifying their actual operation. In this case, the specification mandates the methods, namely getters and setters. CMP entity beans do not declare instance variables to represent persistent data. Instead, the container-managed data element names are declared in the deployment descriptor. In the bean class, public abstract getXXX and setXXX methods are declared for the data elements; the container subclasses the bean class and generates the actual implementation. The familiar JavaBeans™ convention is used: if the data element is named value, the getter/setter pair would be type getValue() and void setValue( type aValue ). So, CMP not only generates the datastore routines, but also manages the individual data elements. This means that the developer can access data only via the getters and setters. The specification calls the data elements virtual fields. The deployment descriptor declaration of the virtual fields is called an abstract persistence schema, and the entries look like this:


...
<cmp-version>2.x</cmp-version>
<abstract-schema-name>ValueBean</abstract-schema-name>
<cmp-field>
<field-name>value</field-name>
</cmp-field>
<cmp-field>
<field-name>cost</field-name>
</cmp-field>
...

     Before moving on to CMP method handling, a couple of other differences between BMP and CMP should be mentioned. One is that the described data elements must be mapped to actual database columns (or whatever the data elements are in the datastore used). This mapping description goes into a vendor-specific container descriptor, usually generated by vendor tools. In the words of the EJB 2.0 specification, "The EJB deployment descriptor ... does not provide a mechanism for specifying how the abstract persistence schema of an entity bean ... is to be mapped to an underlying database. This is the responsibility of the Deployer."

     Another difference between BMP and CMP is that the EJB 2.0 specification introduced the EJB Query Language (QL), a portable subset of SQL92. QL syntax is used to define queries for finder and select methods under CMP. The queries are defined in the deployment descriptor. The QL syntax is normally translated into the access language of the underlying datastore. In version 2.0 CMP, QL is required for all finders except ejbFindByPrimaryKey(). For more information, see Resources.



Tutorial Home Section Home Previous Section Next