Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > MyEclipse+Weblogic8.1+Entity Bean
【标  题】:MyEclipse+Weblogic8.1+Entity Bean
【关键字】:MyEclipse+Weblogic8.1+Entity,Bean
【来  源】:http://blog.csdn.net/Asclepios/archive/2007/04/16/1566655.aspx

MyEclipse+Weblogic8.1+Entity Bean

Your Ad Here

一、配置Eclipse与MyEclipse

省略:去网上找相关资料

二、配置Weblogic8.1

省略:去网上找相关资料

三、配置Weblogic8.1的数据源

  1. 启动Weblogic服务器,打开weblogic的后台管理器(http://127.0.0.1:7001/console) 
  2. 配置Connection Pools,位置:右边菜单树mydomain--Services--JDBC--Connection Pools
  3. 配置完Connection Pools后,配置DataSources,位置:右边菜单树mydomain--Services--JDBC-DataSources
  4. 记住所配置的DataSources的名字,切记所配置的数据源中的数据库是将来配置Entity Bean时能访问到的表。

四、新建Entity Bean

  1. 新建EJB工程。因为是Weblogic8.1所以在选择J2EE版本一定要选择1.3否则就重新建吧
  2. 新建Entity Bean。其中包名需要是ejb结尾,否则MyEclipse会warning的。其它设置随便。在选择EJB类型时选择CMP2.X,选择EJB访问形式时,选择both
  3. 编辑Entity Bean由于MyEclipse生成的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

  1. 选择刚才所建立的工程,选择属性。选择工程右击--propertise
  2. 配置Xdoclet。刚才的对话框,Myeclipse--Xdoclet。在上面的框中右击选择Add Standard出现对话框,选择Standard EJB
  3. 选中刚刚添加的Standard EJB的节点,下面的框中会出现该选项所拥有的列表,选中其中的根节点:ejbdoclet右击Add,在出现的对话框中的选择weblogic
  4. 在同样的框中选中刚才添加的weblogic节点,在右边的属性列表中对以下节点进行修改

version----------8.1(因为我的Weblogic是8.1)

datasource----------刚才在配置weblogic的数据源名

destDir------src/META-INF(就是我们建立工程时src文件夹下面看到的META-INF文件夹)

六、执行Xdoclet

  1. 选中工程右击找到MyEclipse菜单项,选中该菜单项下的Run Xdoclet子菜单,这时候Eclipse会执行Xdoclet
  2. 等执行完毕后,刚才只有一个文件的包下面会出现一批文件
  3. 部署该EJB工程

七、测试

  1. 启动Weblogic,启动完成后如果控制后台没有出现EJB部署成功的信息,需要到Weblogic下面重新部署
  2. 打开http://127.0.0.1:7001/console,在右菜单mydomain--Deployments--EJB Modules,进行Deploy a new EJB Module... 配置
  3. 配置成功后,weblogic会提示Deploy成功(可能需要等一会儿)

测试代码

    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();
        }

    }

Javascript中判断对象的基本类型:【上一篇】
在struts架构下,用数据源连接数据库:【下一篇】
【相关文章】
  • 期待着NetBeans6.0 Milestone9的发行
  • netbeans代码-bootstrap-main
  • EJB 3.0入门:Stateful Session Bean
  • netBeans 中的自定义代码的书写位置
  • JBoss4.20下的第一个实体Bean(巨简单的)
  • netbeans的文件编码
  • EJB 3.0入门:Stateless Session Beans
  • JDK6下调优NetBeans集成开发环境
  • NetBeans5.5与Eclipse3.2.1的比较
  • 标签实现访问控制
  • 【随机文章】
  • 我怎么时候才能下定决心
  • 初学者学好JAVA最关键几点
  • asp.net 上传附件
  • Zvdfasdfsadfa
  • 工程拆分成多个EXE的方案.
  • 常见无线网络基本配置方法透视
  • 解决JasperReport中给querySQL传表名参数的问题
  • hibernate syn template:用于从mapping生成java类
  • Firefox扩展开发之一
  • 一个history的脚本
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.