`
hyshucom
  • 浏览: 809385 次
文章分类
社区版块
存档分类
最新评论

MyEclipse+Weblogic8.1+Entity Bean

 
阅读更多

一、配置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-basedCMP2.xentitybean.Thisclassmustbedeclared
    *publicabstractbecausetheconcreteclasswill
    *beimplementedbytheCMPproviderstooling.
    *
    *TogenerateEJBrelatedclassesusingXDoclet:
    *
    *-AddStandardEJBmoduletoXDocletprojectproperties
    *-CustomizeXDocletconfiguration
    *-RunXDoclet
    *
    *Belowarethexdoclet-relatedtagsneededforthisEJB.
    *
    *@ejb.beanname="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.persistencetable-name="ROLE" //在连接数据库时数据表名,必须是数据库中名字
    ejb:util
    *generate="physical"
    */

    publicabstractclassRoleimplementsEntityBean...{

    /***//**Theentitycontext*/
    privateEntityContextcontext;

    publicRole()...{
    //TODOAuto-generatedconstructorstub
    }


    /***//**
    *TherearezeroormoreejbCreate<METHOD>(...)methods,whosesignaturesmatch
    *thesignaturesofthecreate<METHOD>(...)methodsoftheentitybean?shomeinterface.
    *ThecontainerinvokesanejbCreate<METHOD>(...)methodonanentitybeaninstance
    *whenaclientinvokesamatchingcreate<METHOD>(...)methodontheentitybean?s
    *homeinterface.<br>
    *
    *Theentitybeanprovider?sresponsibilityistoinitializetheinstanceintheejbCreate<
    *METHOD>(...)methodsfromtheinputarguments,usingthegetandsetaccessor
    *methods,suchthatwhentheejbCreate<METHOD>(...)methodreturns,thepersistent
    *representationoftheinstancecanbecreated.<br>
    *
    *Theentitybeanprovidermustnotattempttomodifythevaluesofcmr-fieldsinanejbCreate<
    *METHOD(...)method;thisshouldbedoneintheejbPostCreate<METHOD(...)methodinstead.<br>
    *
    *TheentityobjectcreatedbytheejbCreate<METHOD>methodmusthaveauniqueprimary
    *key.Thismeansthattheprimarykeymustbedifferentfromtheprimarykeysofalltheexisting
    *entityobjectswithinthesamehome.However,itislegaltoreusetheprimarykeyofapreviously
    *removedentityobject.Theimplementationofthebeanprovider?sejbCreate<
    *METHOD>(...)methodsshouldbecodedtoreturnanull.<br>
    *
    *AnejbCreate<METHOD>(...)methodexecutesinthetransactioncontextdeterminedby
    *thetransactionattributeofthematchingcreate<METHOD>(...)method.
    *Thedatabaseinsertoperationsareperformedbythecontainerwithinthesame
    *transactioncontextaftertheBeanProvider?sejbCreate<METHOD>(...)methodcompletes.
    *
    *
    @throwsCreateExceptionThrownifmethodfailsduetosystem-levelerror.
    *
    *
    @throwsCreateException
    *
    *@ejb.create-method
    */

    publicLongejbCreate()throwsCreateException...{
    returnnull;
    }


    /***//**
    *ForeachejbCreate<METHOD>(...)method,thereisamatchingejbPostCreate<
    *METHOD>(...)methodthathasthesameinputparametersbutwhosereturntypeis
    *void.ThecontainerinvokesthematchingejbPostCreate<METHOD>(...)methodon
    *aninstanceafteritinvokestheejbCreate<METHOD>(...)methodwiththesamearguments.
    *TheinstancecandiscovertheprimarykeybycallinggetPrimaryKey()onits
    *entitycontextobject.<br>
    *
    *TheentityobjectidentityisavailableduringtheejbPostCreate<METHOD>(...)
    *method.Theinstancemay,forexample,obtainthecomponentinterfaceoftheassociatedentity
    *objectandpassittoanotherenterprisebeanasamethodargument.<br>
    *
    *TheentityBeanProvidermayusetheejbPostCreate<METHOD>(...)tosetthevalues
    *ofcmr-fieldstocompletetheinitializationoftheentitybeaninstance.
    *AnejbPostCreate<METHOD>(...)methodexecutesinthesametransactioncontextas
    *thepreviousejbCreate<METHOD>(...)method.
    *
    *
    @throwsCreateExceptionThrownifmethodfailsduetosystem-levelerror.
    */

    publicvoidejbPostCreate()throwsCreateException...{
    }


    publicvoidejbActivate()throwsEJBException,RemoteException...{
    //TODOAuto-generatedmethodstub

    }


    publicvoidejbLoad()throwsEJBException,RemoteException...{
    //TODOAuto-generatedmethodstub

    }


    publicvoidejbPassivate()throwsEJBException,RemoteException...{
    //TODOAuto-generatedmethodstub

    }


    publicvoidejbRemove()
    throwsRemoveException,
    EJBException,
    RemoteException
    ...{
    //TODOAuto-generatedmethodstub

    }


    publicvoidejbStore()throwsEJBException,RemoteException...{
    //TODOAuto-generatedmethodstub

    }


    /***//**
    *Settheassociatedentitycontext.Thecontainercallsthismethod
    *aftertheinstancecreation.Theentitybeanmustnotattemptto
    *accessitspersistentstateandrelationshipsusingtheaccessor
    *methodsduringthismethod.<br>
    *
    *Theenterprisebeaninstanceshouldstorethereferencetothecontext
    *objectinaninstancevariable.<br>
    *
    *Thismethodiscalledwithnotransactioncontext.
    *
    *
    @throwsEJBExceptionThrownifmethodfailsduetosystem-levelerror.
    */

    publicvoidsetEntityContext(EntityContextnewContext)throwsEJBException...{
    context
    =newContext;
    }


    /***//**
    *Unsettheassociatedentitycontext.Acontainerinvokesthismethod
    *beforeterminatingthelifeoftheinstance.Theentitybeanmustnot
    *attempttoaccessitspersistentstateandrelationshipsusingthe
    *accessormethodsduringthismethod.<br>
    *
    *Thismethodiscalledwithnotransactioncontext.
    *
    *
    @throwsEJBExceptionThrownifmethodfailsduetosystem-levelerror.
    */

    publicvoidunsetEntityContext()throwsEJBException...{
    context
    =null;
    }

    /***//**
    *@ejb.interface-methodview-type="both"
    *@ejb.persistencecolumn-name="roleId" //表明该方法所对应的数据表名
    *@ejb.pk-field //如果该字段是主键需要加上这么一个字段
    *
    @return
    */

    publicabstractLonggetRoleId();

    /***//**
    *@ejb.interface-methodview-type="both"
    *
    @paramroleId
    */

    publicabstractvoidsetRoleId(LongroleId);

    /***//**
    *@ejb.interface-methodview-type="both"
    *@ejb.persistencecolumn-name="roleName"
    *
    @return
    */

    publicabstractStringgetRoleName();

    /***//**
    *@ejb.interface-methodview-type="both"
    *
    @paramroleName
    */

    publicabstractvoidsetRoleName(StringroleName);
    }

注:在以上代码中类上面的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成功(可能需要等一会儿)

测试代码

publicstaticvoidmain(String[]args)...{
Propertiesproperties
=newProperties();
//Weblogicconfigurationcode
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL,
"t3://localhost:7001");
//t3://localhost:7001weblogicserveraddressandport
try...{
Contextcontext
=newInitialContext(properties);
RoleHomeroleH
=(RoleHome)context.lookup("ejb/Role");
Rolerole
=roleH.findByPrimaryKey(newLong(100001));
System.out.println(
"theRoleNameis"+role.getRoleName());
}
catch(NamingExceptione)...{
e.printStackTrace();
}
catch(RemoteExceptione)...{
e.printStackTrace();
}
catch(FinderExceptione)...{
e.printStackTrace();
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics