Browse DevX
Sign up for e-mail newsletters from DevX


Creating Container-managed Entity Beans with JBoss

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

n Entity Bean is an Enterprise JavaBean (EJB) that represents a persistent object in a relational database. JBoss provides two methods of entity bean persistence, Bean Managed Persistence (BMP) and Container-Managed Persistence (CMP). With BMP, the entity bean developer must implement all the persistence logic. With CMP, the application server manages entity bean persistence; the developer provides interfaces and configuration.

Applications that use entity beans—particularly entity beans that use CMP— quickly become complex. Therefore, to describe the configuration process in detail without getting bogged down by code complexity, I designed the sample application for this article, JBlog, to be functional and illustrative, yet simple. I also wanted a timely example, which led to the idea of blogging. JBlog is a small blogger application that uses CMP entity beans to persist blog content.

What You Need
To build and run the sample JBlog application, you need JBoss 3.2.1, Java 2 SDK 1.4, Java 2 EE SDK 1.3, Ant 1.5, and JUnit 3.8.1.

JBlog's interface consists of two Web pages, one for viewing a blog and one for posting new entries. The interface uses two entity beans: Story for blog entries and Author for content authors. In the rest of this article, you'll see how to construct, configure, and persist those entity beans.

An entity bean using CMP has several interfaces, explained in the following sections. You don't have to implement all the interfaces for every CMP entity bean.

The Home Interface (optional)
External clients use an entity bean's home interface to create, remove, and find instances of the entity bean. You don't need to create a home interface if external clients will not use the bean directly. The home interface defines the following methods:

  • Create. Creates an entity bean instance.
  • Remove. (required) Removes an entity bean instance.
  • Finder methods. Find one or more entity bean instances. Finder method names must start with "find". For a CMP entity bean, the finder method findByPrimaryKey must be defined.
  • Home methods. These methods act like static methods for an entity bean. Home methods can be called from the home interface (remote or local) to execute entity bean methods without creating an instance of the entity bean.
Both the entity beans in JBlog have home interfaces. To see the full code, look at the StoryHome.java and AuthorHome.java files in the JBlog source directory. The main page (index.jsp) uses the AuthorHome interface to find the author "bigt."

// Create AuthorHome. AuthorHome authorHome = (AuthorHome) PortableRemoteObject.narrow( ctx.lookup("ejb/JBlog/Author"), AuthorHome.class); // Find author. Author author = authorHome.findByPrimaryKey("bigt");

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