📄 ch05s08.html
字号:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Creating the Beans</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch05.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05.html" title="Chapter 5. Using container-managed persistence"><link rel="previous" href="ch05s06.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s06.html" title="Container Managed Persistence - CMP"><link rel="next" href="ch05s09.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s09.html" title="Packaging and deploying the Beans"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch05.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch05s06.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s06.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch05s09.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s09.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="d0e1855"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="d0e1855"></a>Creating the Beans</h2></div></div><p>The entity bean representing the CD is very easy to code, as it doesn't have
to do a great deal.
All the issues of persistence will be taken care of by the server. I will
present the full code
for this Bean below; the code for the CDCollection Bean will not be discussed
further because it
is not interesting in the context of container-managed persistence. </p><p>CD.java: remote interface for the "CD" Bean</p><p>
<pre class="programlisting">
package org.jboss.docs.cmp.cd.interfaces;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
/**
* This interface defines the remote interface for the CD Bean
*/
public interface CD
extends EJBObject
{
public Integer getId() throws RemoteException;
public void setId(Integer id) throws RemoteException;
public String getTitle() throws RemoteException;
public void setTitle(String title) throws RemoteException;
public String getArtist() throws RemoteException;
public void setArtist(String artist) throws RemoteException;
public String getType() throws RemoteException;
public void setType(String type) throws RemoteException;
public String getNotes() throws RemoteException;
public void setNotes(String type) throws RemoteException;
}
</pre>
</p><p>
The remote interface specifies methods to get and set the attributes of the
object.
That's all it needs to do in this example. Note that, as with any JavaBean,
the
names of these methods follow the standard convention; that is, if an
attribute
is called "String X" then the "get" and "set" methods should be defined as
follows: </p><pre class="programlisting">String getX();
void setX(String x);</pre><p>However you can declare any methods that return information on
the state of the entity bean. These methods must be implemented by the programmer
in the implementation class of the entity bean.</p><p>CDHome.java: home interface for the "CD" Bean</p><pre class="programlisting">
package org.jboss.docs.cmp.cd.interfaces;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
import java.util.Collection;
/**
* This interface defines the home interface for the CD Bean
*/
public interface CDHome
extends EJBHome
{
/**
* Create a new CD instance
*/
public CD create(Integer id)
throws RemoteException, CreateException;
/**
* Find the CD with the specified ID. This method is implemented by the
* container
*/
public CD findByPrimaryKey (Integer id)
throws RemoteException, FinderException;
/**
* Finds the CD whose "type" attribute matches that specified.
* This method is implemented by the container
*/
public Collection findByType (String type)
throws RemoteException, FinderException;
/**
* Get all CD instances. This method is implemented by the container
*/
public Collection findAll()
throws RemoteException, FinderException;
}
</pre><p>
The important thing to note about this interface is that it specifies methods
that
don't need to be implemented. In this case, findByPrimaryKey(), findByType()
and findAll()
are all examples of "finder" methods. The EJB specification requires that the
server be able
to provide finder methods for all the persistent attributes in the object. So,
for example,
if your class has an attribute "X", then the server will provide a "findByX"
method to search
on that field. Note that with JBoss the search is "exact"; that is, it won't
accept wild-card
characters or an incorrect mixture of upper- and lower-case letters. The
findByPrimaryKey()
searches on the primary key field; we will discuss how the primary key is
specified later.</p><p>CDBean.java: implementation of the "CD" Bean</p><p>
<pre class="programlisting">
package org.jboss.docs.cmp.cd.bean;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import java.rmi.RemoteException;
/**
* This class contains the implementation for the methods specified in the home
* and remote interfaces for the "CD" EJB
*/
public class CDBean
implements EntityBean
{
EntityContext ctx;
public Integer id;
public String title;
public String artist;
public String type;
public String notes;
/**
* Create an instance of a CD. Note that this method returns null because the real
* creation is managed by the EJB container.
*/
public Integer ejbCreate (Integer _id)
{
id = _id;
return null;
}
/**
* Called when the object has been instantiated; does nothing in this example
*/
public void ejbPostCreate(Integer id) { }
public String getTitle() { return title; }
public void setTitle(String _title) { title = _title; }
public Integer getId() { return id; }
public void setId(Integer _id) { id = _id; }
public String getArtist() { return artist; }
public void setArtist(String _artist) { artist = _artist; }
public String getType() { return type; }
public void setType(String _type) { type = _type; }
public String getNotes() { return notes; }
public void setNotes(String _notes) { notes = _notes; }
public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }
public void unsetEntityContext() { ctx = null; }
public void ejbActivate() { }
public void ejbPassivate() { }
public void ejbLoad() { }
public void ejbStore() { }
public void ejbRemove() { }
}
</pre>
</p><p>
The CDBean class provides implementations of the methods that aren't provided
automatically
by the EJB container. Note that the ejbCreate method returns "null", meaning
that the
container should take care of initializing the instance in the server's
process space.
Because the CD Bean is essentially passive -- a data repository -- it only has
a few methods. </p></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch05.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch05s06.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s06.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch05s09.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch05s09.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -