top
Loading...
完善Bean實體
接著我們就必須來完成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} );
}
}

我們將這段代碼存入hdsitesrcjavaetchinacodeaddressbookAddressEntryBean.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會依照這個說明自動的去創建數據庫同時去數據庫中進行查詢。
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗