Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Container-managed Entity Beans with JBoss : Page 2

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.


advertisement
The Remote Interface (optional)
External clients interact with an entity bean via its remote interface, which defines the entity bean's public business methods. If the entity bean will not be used by external clients, you don't need to create a remote interface.

Both of the entity beans in JBlog have remote interfaces. To see the code for them, look at Story.java and Author.java in the JBlog source directory. The main page uses Author to get an author's stories and Story to display a story.

// Get an author's stories // Create AuthorHome and StoryHome. AuthorHome authorHome = (AuthorHome) PortableRemoteObject.narrow( ctx.lookup("ejb/JBlog/Author"), AuthorHome.class); StoryHome storyHome = (StoryHome) PortableRemoteObject.narrow( ctx.lookup("ejb/JBlog/Story"), StoryHome.class); // Find author and get stories. Author author = authorHome.findByPrimaryKey("bigt"); Collection stories = author.getStoryKeys(); <% // Display stories. for (Iterator i = stories.iterator(); i.hasNext(); ) { Integer key = (Integer) i.next(); Story story = storyHome.findByPrimaryKey(key); %> <p> <font size="+2"><%= story.getTitle() %></font><br> <b>Posted <%= story.getPubDate() %></b><br> <%= story.getText() %> </p> ...

The posting page (post.jsp) uses Author to assign an author to a new story.

// Assign author to new story // Find author. Author author = authorHome.findByPrimaryKey("bigt"); // Create story. Story story = storyHome.create(request.getParameter("title"), request.getParameter("text"), author);

The Local Home Interface (required)
Local clients—other components running in the same container as the entity bean—use the local home interface to create, remote, and find instances of an entity bean. Like the home interface, the local home interface defines create, remove, finder methods, and home methods. Additionally, the local home interface may define select methods, which are local query methods. You must create a local home interface for a CMP entity bean.

To see the code for the local home interfaces of JBlog's entity beans, look at the files AuthorLocalHome.java and StoryLocalHome.java in the JBlog source directory. These interfaces are referenced in the EJB jar descriptor and the JBoss CMP descriptor and are used by JBoss.

The Local Interface (required)
Local clients interact with an entity bean via its local interface, which defines the local business methods of the entity bean. You must create a local interface for a CMP entity bean.

To see the code for the local interfaces of JBlog's entity beans, look at the AuthorLocal.java and StoryLocal.java files in the JBlog source directory. These interfaces are referenced in the EJB jar descriptor and the JBoss CMP descriptor and are used by JBoss. Additionally, AuthorBean uses the StoryLocal interface to get a list of the primary keys of the author's stories.



// Get primary keys of author's stories public Collection getStoryKeys() { Vector keys = new Vector(); Collection stories = getStories(); // List of StoryLocal instances for (Iterator i = stories.iterator(); i.hasNext(); ) { StoryLocal storyLocal = (StoryLocal) i.next(); keys.add(0, storyLocal.getPrimaryKey()); } return keys; }

Similarly, StoryBean uses the AuthorLocal interface to assign an author to a story.

// Assign author (local) to story // This method is called by the container after entity bean is created. public void ejbPostCreate(String title, String text, AuthorLocal author) { setAuthor(author); }





Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap