一、配置Eclipse与MyEclipse
省略:去网上找相关资料
二、配置Weblogic8.1
省略:去网上找相关资料
三、配置Weblogic8.1的数据源
四、新建Entity Bean

/** *//**
* XDoclet-based CMP 2.x entity bean. This class must be declared
* public abstract because the concrete class will
* be implemented by the CMP providers tooling.
*
* To generate EJB related classes using XDoclet:
*
* - Add Standard EJB module to XDoclet project properties
* - Customize XDoclet configuration
* - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="Role" //你的Entity Bean的名字
display-name="Role" //在生成ejb-jar.xml时的displayname
description="Role"
* jndi-name="ejb/Role" //远程访问该EJB是的JNDI名字,这个在调用的时候需要的
type="CMP" //该Entity Bean的类型
cmp-version="2.x" //CMP的版本
view-type="both" //访问该EJB的形式,both表示Local和Remote都支持
primkey-field = "roleId" //在连接数据表时的主键字段
@ejb.persistence table-name = "ROLE" //在连接数据库时数据表名,必须是数据库中名字
ejb:util
* generate="physical"
*/
public abstract class Role implements EntityBean ...{

/** *//** The entity context */
private EntityContext context;

public Role() ...{
// TODO Auto-generated constructor stub
}

/** *//**
* There are zero or more ejbCreate<METHOD>(...) methods, whose signatures match
* the signatures of the create<METHOD>(...) methods of the entity bean?s home interface.
* The container invokes an ejbCreate<METHOD>(...) method on an entity bean instance
* when a client invokes a matching create<METHOD>(...) method on the entity bean?s
* home interface.<br>
*
* The entity bean provider?s responsibility is to initialize the instance in the ejbCreate<
* METHOD>(...) methods from the input arguments, using the get and set accessor
* methods, such that when the ejbCreate<METHOD>(...) method returns, the persistent
* representation of the instance can be created. <br>
*
* The entity bean provider must not attempt to modify the values of cmr-fields in an ejbCreate<
* METHOD(...) method; this should be done in the ejbPostCreate<METHOD(...) method instead.<br>
*
* The entity object created by the ejbCreate<METHOD> method must have a unique primary
* key. This means that the primary key must be different from the primary keys of all the existing
* entity objects within the same home. However, it is legal to reuse the primary key of a previously
* removed entity object. The implementation of the bean provider?s ejbCreate<
* METHOD>(...) methods should be coded to return a null.<br>
*
* An ejbCreate<METHOD>(...) method executes in the transaction context determined by
* the transaction attribute of the matching create<METHOD>(...) method.
* The database insert operations are performed by the container within the same
* transaction context after the Bean Provider?s ejbCreate<METHOD>(...) method completes.
*
* @throws CreateException Thrown if method fails due to system-level error.
*
* @throws CreateException
*
* @ejb.create-method
*/
public Long ejbCreate() throws CreateException ...{
return null;
}

/** *//**
* For each ejbCreate<METHOD>(...) method, there is a matching ejbPostCreate<
* METHOD>(...) method that has the same input parameters but whose return type is
* void. The container invokes the matching ejbPostCreate<METHOD>(...) method on
* an instance after it invokes the ejbCreate<METHOD>(...) method with the same arguments.
* The instance can discover the primary key by calling getPrimaryKey() on its
* entity context object. <br>
*
* The entity object identity is available during the ejbPostCreate<METHOD>(...)
* method. The instance may, for example, obtain the component interface of the associated entity
* object and pass it to another enterprise bean as a method argument.<br>
*
* The entity Bean Provider may use the ejbPostCreate<METHOD>(...) to set the values
* of cmr-fields to complete the initialization of the entity bean instance.
* An ejbPostCreate<METHOD>(...) method executes in the same transaction context as
* the previous ejbCreate<METHOD>(...) method.
*
* @throws CreateException Thrown if method fails due to system-level error.
*/
public void ejbPostCreate() throws CreateException ...{
}

public void ejbActivate() throws EJBException, RemoteException ...{
// TODO Auto-generated method stub
}

public void ejbLoad() throws EJBException, RemoteException ...{
// TODO Auto-generated method stub
}

public void ejbPassivate() throws EJBException, RemoteException ...{
// TODO Auto-generated method stub
}
public void ejbRemove()
throws RemoveException,
EJBException,
RemoteException ...{
// TODO Auto-generated method stub
}

public void ejbStore() throws EJBException, RemoteException ...{
// TODO Auto-generated method stub
}

/** *//**
* Set the associated entity context. The container calls this method
* after the instance creation. The entity bean must not attempt to
* access its persistent state and relationships using the accessor
* methods during this method. <br>
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setEntityContext(EntityContext newContext) throws EJBException ...{
context = newContext;
}

/** *//**
* Unset the associated entity context. A container invokes this method
* before terminating the life of the instance. The entity bean must not
* attempt to access its persistent state and relationships using the
* accessor methods during this method. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void unsetEntityContext() throws EJBException ...{
context = null;
}
/** *//**
* @ejb.interface-method view-type = "both"
* @ejb.persistence column-name = "roleId" //表明该方法所对应的数据表名
* @ejb.pk-field //如果该字段是主键需要加上这么一个字段
* @return
*/
public abstract Long getRoleId();

/** *//**
* @ejb.interface-method view-type = "both"
* @param roleId
*/
public abstract void setRoleId(Long roleId);

/** *//**
* @ejb.interface-method view-type = "both"
* @ejb.persistence column-name = "roleName"
* @return
*/
public abstract String getRoleName();

/** *//**
* @ejb.interface-method view-type = "both"
* @param roleName
*/
public abstract void setRoleName(String roleName);
}
注:在以上代码中类上面的javadoc红字表示解释和需要自己添加的代码。并且下面方法上面的Javadoc也是需要自己添加的同时这些方法,也得自己动手,丰衣足食(MyEclipse的Entity Bean做的非常不好,几乎什么都要自己添加,大概MyEclipse觉得Entity Bean马上就要被ORM的框架取代了)
以上我们Entity Bean的代码阶段就完成了,下面就是配置Xdoclet
五、配置Xdoclet
version----------8.1(因为我的Weblogic是8.1)
datasource----------刚才在配置weblogic的数据源名
destDir------src/META-INF(就是我们建立工程时src文件夹下面看到的META-INF文件夹)
六、执行Xdoclet
七、测试
测试代码

public static void main(String[] args) ...{
Properties properties = new Properties();
//Weblogic configuration code
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
//t3://localhost:7001 weblogic server address and port
try ...{
Context context = new InitialContext(properties);
RoleHome roleH = (RoleHome) context.lookup("ejb/Role");
Role role = roleH.findByPrimaryKey(new Long(100001));
System.out.println("the Role Name is " + role.getRoleName());
} catch (NamingException e) ...{
e.printStackTrace();
} catch (RemoteException e) ...{
e.printStackTrace();
} catch (FinderException e) ...{
e.printStackTrace();
}
}