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 clientsother components running in the same container as the entity beanuse 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);
}