網域查詢: www.
返回首頁
當前位置: 首頁 > 站長學院 > 網站編程 > JSP教程 >

完善Bean實體

時間:2010-02-17 04:48來源: 作者: 點擊:
接著我們就必須來完成Bean實體自己了。這個實體所做的工作與session bean中的工做大致相同,但是它的繼承的父類不是session bean了而是EntiyBean了。具體代碼如下︰ package net.chinacode.address
  接著我們就必須來完成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會依照這個說明自動的去創建數據庫同時去數據庫中進行查詢。
頂一下
(0)
0%
踩一下
(0)
0%
------分隔線----------------------------
最新評論 查看所有評論
發表評論 查看所有評論
請自覺遵守互聯網相關的政策法規,嚴禁發佈色情、暴力、反動的言論。
評價:
表情:
用戶名: 密碼: 驗證碼:
推薦內容