接着我们就必须来完成Bean实体自己了。这个实体所做的工作与session bean中的工做大致相同,但是它的继承的父类不是session bean了而是EntiyBean了。具体代码如下: package net.chinacode.addressbook; import javax.ejb.*; import java.rmi.*; public class AddressEntryBean extends Object implements EntityBean { public static int instanceCount = 0; private transient TraceHelper tracer; public AddressEntryBean() { int instanceNr = instanceCount++; tracer = new TraceHelper("AddressEntryBean[" + instanceCount + ']'); tracer.trace(" "); }
public String name; public String address; public String city;
public String getName() { tracer.trace("getName", name); return name; }
public String getAddress() { tracer.trace("getAddress", address); return address; }
public String getCity() { tracer.trace("getCity", city); return city; }
public void setAddress(String newAddress) { tracer.trace("setAddress", new String[] { newAddress }); address = newAddress; }
public void setCity(String newCity) { tracer.trace("setCity", new String[] { newCity }); city = newCity; }
public void ejbActivate() { tracer.trace("ejbActivate"); }
public void ejbStore() { tracer.trace("ejbStore"); }
public void setEntityContext(EntityContext entityContext) { tracer.trace("setEntityContext", new String[] { String.valueOf(entityContext) }); }
public void unsetEntityContext() { tracer.trace("unsetEntityContext"); }
public void ejbPassivate() { tracer.trace("ejbPassivate"); }
public void ejbLoad() { tracer.trace("ejbLoad"); }
public void ejbRemove() { tracer.trace("ejbRemove"); }
public String ejbCreate(String initialName, String initialAddress, String initialCity) throws CreateException, RemoteException { tracer.trace("ejbCreate", new String[] { initialName, initialAddress, initialCity }, initialName); name = initialName; address = initialAddress; city = initialCity; return initialName; }
public void ejbPostCreate(String initialName, String initialAddress, String initialCity) throws CreateException, RemoteException { tracer.trace("ejbPostCreate", new String[] { initialName, initialAddress, initialCity} ); } }
我们将这段代码存入hdsite\src\java\net\chinacode\addressbook\AddressEntryBean.java文件中。这里使用了一个tracer的TraceHelper类,它只是用来向orion终端输入调试信息的。除去了tracer的代码,哪么剩余的代码已经变的很少了,而且好像没有任何操作的地方。这就是EJB帮助我们完成了所有的事。
以下列出TraceHelper类的trace方法的代码,由于篇幅所限这里不列出全的代码了:
private void trace(String methodName, String[] arguments, String returnValue, boolean withReturnValue) { final DateFormat formatter = new SimpleDateFormat("hh:mm:ss"); Date now = new Date(); stream.print(formatter.format(now)); stream.print(' '); stream.print(name); stream.print(": "); stream.print(methodName); stream.print('(');
int count = (arguments == null) ? 0 : arguments.length; if (count > 0) { if (arguments[0] == null) { stream.print("null"); } else { stream.print('"'); stream.print(arguments[0]); stream.print('"'); } } for (int i=1; i stream.print(", "); if (arguments[i] == null) { stream.print("null"); } else { stream.print('"'); stream.print(arguments[i]); stream.print('"'); } } stream.print(')');
if (withReturnValue) { stream.print(" -> ");
if (returnValue == null) { stream.print("null"); } else { stream.print('"'); stream.print(returnValue); stream.print('"'); } }
stream.println(); }
这些完成后我们就完成了一个完整的实体Bean。具体与数据库交互时需要向数据库提交的信息我们必须写在配置文件中,如下:
address book entry bean Address book entry net.chinacode.addressbook.AddressEntry net.chinacode.addressbook.AddressBook net.chinacode.addressbook.AddressEntry net.chinacode.addressbook.AddressEntryBean Container name java.lang.String False name address city
我们将这段xml加入到之前我们写好的ejb-jar.xml文件的enterprise-bean段中去。我们可以看到在这里我们不但如之前描述session bean一样的说明了EJB的结构,而且还说明了这个实体Bean的存在数据库中的字段,以当这些字段中哪些字段为主键。Orion会依照这个说明自动的去创建数据库同时去数据库中进行查询。
|