阅读文章(首页/程序开发/JSP教程/)

完善Bean实体

[日期:2001-04-02] 来源:yesky  作者:黄冬 [字体: ]

  接着我们就必须来完成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会依照这个说明自动的去创建数据库同时去数据库中进行查询。



阅读:
录入:

评论 】 【 推荐 】 【 打印
上一篇:Cookie简介及JSP处理Cookie的方法
下一篇:建立entity bean
相关文章      
本文评论
发表评论


点评: 字数
姓名:

  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款