首先是增加entityBean对应的三个类:
com.rox.HelloEntity(EntityBean)
com.rox.HelloEntityHome(EJBHome)
com.rox.HelloEntityInterface(EJBObject)
三个类对应的代码如下:
HelloEntity.java:
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;
public abstract class HelloEntity implements EntityBean {
/** The entity context */
private EntityContext context;
public abstract Integer getHelloNo();
public abstract void setHelloNo(Integer s);
public abstract String getHelloName();
public abstract void setHelloName(String s);
public String getHello()
{
return getHelloName();
}
public HelloEntity() {
super();
// TODO Auto-generated constructor stub
}
private EntityContext context;
public abstract Integer getHelloNo();
public abstract void setHelloNo(Integer s);
public abstract String getHelloName();
public abstract void setHelloName(String s);
public String getHello()
{
return getHelloName();
}
public HelloEntity() {
super();
// TODO Auto-generated constructor stub
}
public Integer ejbCreate() throws CreateException {
setHelloNo(1);
setHelloName("my god");
return null;
}
public void ejbPostCreate() throws CreateException {
}
public void setEntityContext(EntityContext newContext) throws EJBException {
context = newContext;
}
context = newContext;
}
public void unsetEntityContext() throws EJBException {
context = null;
}
context = null;
}
public void ejbRemove()
throws RemoveException,
EJBException,
RemoteException {
throws RemoveException,
EJBException,
RemoteException {
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
public void ejbStore() throws EJBException, RemoteException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
}
HelloEntityHome.java:
package com.rox;
public interface HelloEntityHome
extends javax.ejb.EJBHome
{
public com.rox.HelloEntityInterface create()
throws javax.ejb.CreateException,java.rmi.RemoteException;
throws javax.ejb.CreateException,java.rmi.RemoteException;
public com.rox.HelloEntityInterface findByPrimaryKey(Integer pk)
throws javax.ejb.FinderException,java.rmi.RemoteException;
throws javax.ejb.FinderException,java.rmi.RemoteException;
}
HelloEntityInterface.java:
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface HelloEntityInterface extends EJBObject
{
public String getHello() throws RemoteException;
{
public String getHello() throws RemoteException;
}
这三个类的大部分方法都是继承自ejb对象的接口。
例如:
ejbActivate
ejbPassivate
ejbRemove
ejbStore
ejbLoad
setEntityContext
unsetEntityContext
只有唯一的一个业务方法:public String getHello(),在HelloEntityInterface 中定义,在HelloEntity中具体实现.
注意在HelloEntity中定义了几个抽象的get,set方法,这些方法对应的其实是持久化的属性(对于数据库开说是表的字段)。这也是ejb的规范约定的。
写完这三个类之后,接下来就是配置文件,也是三个。
ejb-jar.xml
jboss.xml
jbosscmp-jdbc.xml
在ejb-jar.xml中添加:
在jboss.xml中添加:
jbosscmp-jdbc.xml是新增加的配置文件,它的内容如下:
http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">
可以看出jbosscmp-jdbc是bean中属性和表的属性的对应关系,实际上我们并不需要建立表 ,ejb会通过配置文件建立表.可以在deploy目录下发现
下会发现各种数据库连接文件的例子.
hsqldb-ds.xml,这是jboss中定义的数据库连接文件.hsqldb说明是与hsqldb相连.在{JBOSS_HOME}\docs\examples\jca