創(chuàng)建實(shí)體Bean 和上篇實(shí)戰(zhàn)EJB一樣,我們?cè)贘builder中先建立一個(gè)guestbook.jpx項(xiàng)目,然后再利用向?qū)ЫJB名為book,接著我們需要建立它的數(shù)據(jù)源,也命名為book,當(dāng)然你想取什么名字都行,不過(guò)以后在weblogic中的數(shù)據(jù)源配置要和這里寫(xiě)的一樣,由于這些配置和上一篇相同,所以這里我都省去了,請(qǐng)大家仔細(xì)看上篇的貼子,有問(wèn)題請(qǐng)PM我! 首先創(chuàng)建book表的CMP實(shí)體Bean文件,由于ID是在數(shù)據(jù)庫(kù)中自動(dòng)增加,在此我們可以將它刪除,而指定name為主鍵,同時(shí)我們修改posttime的類型為java.lang.String.
接下來(lái)我們?cè)O(shè)計(jì)模型,這里放在value中,下面是BookValue.java的源代碼
package guestbook.maxuan.ejb.entity.value;
import java.io.*;
public class BookValue implements Serializable{ public java.lang.String name; public java.lang.String email; public java.lang.String qq; public java.lang.String posttime; public java.lang.String comment; public java.lang.String ip; public java.lang.String action; private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException,
IOException { ois.defaultReadObject(); } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public java.lang.String getPosttime() { return posttime; } public void setPosttime(java.lang.String posttime) { this.posttime = posttime; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
book.java的源代碼
package guestbook.maxuan.ejb.entity; import javax.ejb.*; import java.util.*; import guestbook.maxuan.ejb.entity.value.*;
public interface Book extends javax.ejb.EJBLocalObject { public void setName(String name); public String getName(); public void setEmail(String email); public String getEmail(); public void setQq(String qq); public String getQq(); public void setPosttime(String posttime); public String getPosttime(); public void setComment(String comment); public String getComment(); public void setIp(String ip); public String getIp(); public BookValue getValue(); public void setValue(BookValue value); }
BookBean.java的源代碼 package guestbook.maxuan.ejb.entity;
import javax.ejb.*; import guestbook.maxuan.ejb.entity.value.*;
abstract public class BookBean implements EntityBean { EntityContext entityContext; public java.lang.String ejbCreate(java.lang.String name) throws CreateException { setName(name); return null; } public java.lang.String ejbCreate(BookValue value) throws CreateException { setValue(value); return null; } public void ejbPostCreate(java.lang.String name) throws CreateException { /**@todo Complete this method*/ } public void ejbPostCreate(BookValue value) throws CreateException { } public void ejbRemove() throws RemoveException { /**@todo Complete this method*/ } public abstract void setName(java.lang.String name); public abstract void setEmail(java.lang.String email); public abstract void setQq(java.lang.String qq); public abstract void setPosttime(java.lang.String posttime); public abstract void setComment(java.lang.String comment); public abstract void setIp(java.lang.String ip); public abstract java.lang.String getName(); public abstract java.lang.String getEmail(); public abstract java.lang.String getQq(); public abstract java.lang.String getPosttime(); public abstract java.lang.String getComment(); public abstract java.lang.String getIp(); public void ejbLoad() { /**@todo Complete this method*/ } public void ejbStore() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void unsetEntityContext() { this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public BookValue getValue(){ BookValue value = new BookValue(); value.setName(getName()); value.setEmail(getEmail()); value.setQq(getQq()); value.setPosttime(getPosttime()); value.setComment(getComment()); value.setIp(getIp()); return value; } public void setValue(BookValue value){ setName(value.getName()); setEmail(value.getEmail()); setQq(value.getQq()); setPosttime(value.getPosttime()); setComment(value.getComment()); setIp(value.getIp()); } }
BookHome.java的源代碼
package guestbook.maxuan.ejb.entity;
import javax.ejb.*; import java.util.*; import guestbook.maxuan.ejb.entity.value.*;
public interface BookHome extends javax.ejb.EJBLocalHome { public Book create(String name) throws CreateException; public Book create(BookValue value) throws CreateException; public Collection findAll() throws FinderException; public Book findByPrimaryKey(String name) throws FinderException; }
記得要寫(xiě)findall的Query語(yǔ)句:SELECT OBJECT(o) FROM Book AS o 接下來(lái)我們?cè)O(shè)計(jì)模型,這里放在value中,下面是BookValue.java的源代碼
<textarea columns=100 rows=50>
package guestbook.maxuan.ejb.entity.value;
import java.io.*;
public class BookValue implements Serializable{ public java.lang.String name; public java.lang.String email; public java.lang.String qq; public java.lang.String posttime; public java.lang.String comment; public java.lang.String ip; public java.lang.String action; private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException,
IOException { ois.defaultReadObject(); } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public java.lang.String getPosttime() { return posttime; } public void setPosttime(java.lang.String posttime) { this.posttime = posttime; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
</textarea>
book.java的源代碼
<textarea columns=100 rows=50>
package guestbook.maxuan.ejb.entity;
import javax.ejb.*; import java.util.*; import guestbook.maxuan.ejb.entity.value.*;
public interface Book extends javax.ejb.EJBLocalObject { public void setName(String name); public String getName(); public void setEmail(String email); public String getEmail(); public void setQq(String qq); public String getQq(); public void setPosttime(String posttime); public String getPosttime(); public void setComment(String comment); public String getComment(); public void setIp(String ip); public String getIp(); public BookValue getValue(); public void setValue(BookValue value); }
</textarea> BookBean.java的源代碼
<textarea columns=100 rows=50>
package guestbook.maxuan.ejb.entity;
import javax.ejb.*; import guestbook.maxuan.ejb.entity.value.*;
abstract public class BookBean implements EntityBean { EntityContext entityContext; public java.lang.String ejbCreate(java.lang.String name) throws CreateException { setName(name); return null; } public java.lang.String ejbCreate(BookValue value) throws CreateException { setValue(value); return null; } public void ejbPostCreate(java.lang.String name) throws CreateException { /**@todo Complete this method*/ } public void ejbPostCreate(BookValue value) throws CreateException { } public void ejbRemove() throws RemoveException { /**@todo Complete this method*/ } public abstract void setName(java.lang.String name); public abstract void setEmail(java.lang.String email); public abstract void setQq(java.lang.String qq); public abstract void setPosttime(java.lang.String posttime); public abstract void setComment(java.lang.String comment); public abstract void setIp(java.lang.String ip); public abstract java.lang.String getName(); public abstract java.lang.String getEmail(); public abstract java.lang.String getQq(); public abstract java.lang.String getPosttime(); public abstract java.lang.String getComment(); public abstract java.lang.String getIp(); public void ejbLoad() { /**@todo Complete this method*/ } public void ejbStore() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void unsetEntityContext() { this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public BookValue getValue(){ BookValue value = new BookValue(); value.setName(getName()); value.setEmail(getEmail()); value.setQq(getQq()); value.setPosttime(getPosttime()); value.setComment(getComment()); value.setIp(getIp()); return value; } public void setValue(BookValue value){ setName(value.getName()); setEmail(value.getEmail()); setQq(value.getQq()); setPosttime(value.getPosttime()); setComment(value.getComment()); setIp(value.getIp()); } } </textarea> BookHome.java的源代碼 <textarea columns=100 rows=50> package guestbook.maxuan.ejb.entity; import javax.ejb.*; import java.util.*; import guestbook.maxuan.ejb.entity.value.*;
public interface BookHome extends javax.ejb.EJBLocalHome { public Book create(String name) throws CreateException; public Book create(BookValue value) throws CreateException; public Collection findAll() throws FinderException; public Book findByPrimaryKey(String name) throws FinderException; } </textarea> 記得要寫(xiě)findall的Query語(yǔ)句:SELECT OBJECT(o) FROM Book AS o
出處:藍(lán)色理想
責(zé)任編輯:藍(lán)色
上一頁(yè) [J2EE] 實(shí)戰(zhàn)留言本 [2] 下一頁(yè) [J2EE] 實(shí)戰(zhàn)留言本 [4]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|