Browse DevX
Sign up for e-mail newsletters from DevX


Creating Container-managed Entity Beans with JBoss : Page 3

Entity JavaBeans that use container-managed persistence (CMP) are convenient, because they require so little custom code to achieve automatic persistence. But that convenience carries a price: beans using CMP are also ferociously complex to configure, and often difficult to debug. In this article, you will see how to create, configure, and deploy entity beans that use CMP and JBoss.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Entity Bean Class
The entity bean class contains the entity bean logic. However, with CMP, the entity class is abstract, because many of the methods are defined in the class but implemented by the container. The entity bean class defines the following methods:

  • Accessor methods. (required) Get and set the entity bean's fields. Accessor methods must be both public and abstract, and must match the <field-name> of a <cmp-field> or <cmr-field> in the EJB jar descriptor, using JavaBean naming conventions. For example, the field "name" must have the following accessor methods:

public abstract void setName(String name) public abstract String getName()

  • EJB home methods. Implement the home methods defined in the home interface and the local home interface. Home method names must start with "ejbHome".
  • EJB select methods. Find one or more local entity bean instances. Select methods are mapped to EJB-QL queries in the EJB jar descriptor. Select method names must start with "ejbSelect".
  • Business methods. Implement any business methods defined in the remote and local interfaces.
To see the code for the entity bean classes in JBlog, look at AuthorBean.java and StoryBean.java in the JBlog source directory. AuthorBean and StoryBean are referenced in the EJB jar descriptor and the JBoss CMP descriptor.

The EJB Jar Descriptor
The EJB jar descriptor file, ejb-jar.xml, specifies deployment information for the EJBs in an EJB jar. For each entity bean, the descriptor must contain one <entity> child element in the <enterprise-beans> element. The <entity> element contains the following child elements:

  • <display-name> (required). Display name of entity bean.
  • <ejb-name> (required). Entity bean name. For CMP, this name must be a valid Java identifier.
  • <home>. Fully qualified name of home interface.
  • <remote>. Fully qualified name of remote interface.
  • <local-home> (required). Fully qualified name of local home interface.
  • <local> (required). Fully qualified name of local interface.
  • <ejb-class>(required). Fully qualified name of entity bean class.
  • <reentrant>. Indicator that the bean is reentrant.
  • <persistence-type>. Must be "Container" for CMP.
  • <cmp-version>. Must be "2.x" for CMP2.
  • <abstract-schema-name>. Name of table in elements.
  • <primkey-class>. Fully qualified name of primary key class, for example, "java.lang.String".
  • <prim-key-field>. Name of primary key field.
  • <cmp-field>. Container managed field. The field name is specified with <field-name> and should correspond to accessor methods in the entity bean class.
  • <query>. Query for a finder method or select method in EJB Query Language (EJB-QL). The method name and parameters are specified in <query-method>. The query is specified in <ejb-ql>.
EJB-QL is beyond the scope of this article. For more information on EJB-QL, see the EJB 2.0 specification.

JBlog's EJB jar descriptor contains two <entity> elements, one for Author (see Listing 1) and one for Story (see Listing 2).

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date