3. 将实体EJB及相应的部署描述文件打包成为一个jar文件
4. 部署此实体EJB
一个实体EJB至少由以下三个类(接口)组成:
组件接口――在本例中我们只考虑从同一JVM虚拟机中访问实体EJB,因此我们需要继承javax.ejb.EJBLocalObject接口。
2. package com.jeffhanson.datatier.ejb;
3.
4. import javax.ejb.EJBLocalObject;
5.
6. public interface LocalUser extends EJBLocalObject
7. {
8. public String getUserID(); //主键
9. public String getFullName();
10. public String setAddress(String address);
11. public String getAddress();
12. public String setCity(String city);
13. public String getCity();
14. public String setState(String state);
15. public String getState();
16. public String setZip(String zip);
17. public String getZip();
}
18. Home接口――同样,由于我们处于同一JVM虚拟机中,因此我们需要继承javax.ejb.EJBLocalHome接口。
19. package com.jeffhanson.datatier.ejb;
20.
21. import javax.ejb.CreateException;
22. import javax.ejb.FinderException;
23. import javax.ejb.EJBLocalHome;
24. import java.util.Collection;
25. public interface LocalUserHome extends EJBLocalHome
26. {
27. public LocalUser create(String userID,
28. String fullName,
29. String address,
30. String city,
31. String state,
32. String zip)
33. throws CreateException;
34.
35. public Collection findByFullName(String fullName)
36. throws FinderException;
37.
38. public LocalUser findByPrimaryKey(String userID)
39. throws FinderException;
}
关于EJB的持久化对象技术分析(4)
时间:2010-12-04
Bean类――如果你要开发会话EJB,那么需要实现javax.ejb.SessionBean接口,如果是实体EJB,那么需要实现javax.ejb.EntityBean接口(参见列表1)。
列表1. EJB类
package com.jeffhanson.datatier.ejb;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import java.util.Locale;
public class UserEJB
implements EntityBean
{
// 地区缺省设为美国英语
private Locale locale = Locale.US;
transient private EntityContext ctx;
public String USERID;
public String FULLNAME;
public String ADDRESS;
public String CITY;
public String STATE;
public String ZIP;
public UserEJB()
{}
public void setLocale(Locale locale)
{
this.locale = locale;
}
//访问CMP域相关的方法
public void setUserID(String userID)
{
USERID = userID;
}
public String getUserID() //主键
{
return USERID;
}
public void setFullName(String fullName)
{
FULLNAME = fullName;
}
public String getFullName()
{
return FULLNAME;
}
public void setAddress(String address)
{
ADDRESS = address;
}
public String getAddress()
{
return ADDRESS;
}
public void setCity(String city)
{
CITY = city;
}
public String getCity()
{
return CITY;
}
public void setState(String state)
{
STATE = state;
}
public String getState()
{
return STATE;
}
public void setZip(String zip)
{
ZIP = zip;
}
public String getZip()
{
return ZIP;
}
public String ejbCreate(String userID,
String fullName,
String address,
String cit
|