posts - 0,  comments - 6,  trackbacks - 0
            圖改日加上,公司上網速度讓人哪個漢阿

          數據庫表用的是oracle的scott示范帳號下dept
          創建一個EJB工程

          1.        File > New > Project.

          或在Package Explorer 中點右鍵,單擊new,選擇Project

          ctrl+new 選擇 EJB Project

          2.        選擇J2EE目錄下 EJB Project

          3.        選擇 Next >.

          輸入工程名稱,如dept ,單擊finish完成。

          2    創建一個CMP EJB2 Entity Bean

          1.        主界面選擇菜單File > New > Other,打開新建向導。

          或在Package Explorer 中點右鍵,單擊new,選擇Other

          ctrl+new 選擇 MyEclipse > ejb>EJB2 Entity Bean

          2.        展開MyEclipse > ejb 文件夾,選擇> EJB2 Entity Bean

          3.        選擇Next>,界面如下。這里要注意,package建議用.ejb后綴,因為XDoclet工具默認ejb bean所在的文件夾以.ejb后綴,接口文件的文件夾以.interface為后綴,為了避免設置上的麻煩,建議按默認情況取名。當然你也可以通過設置XDoclet屬性改變,詳情請查看幫助文檔。

          4.             輸入package : com.entity.cmp.ejb

          5.             輸入ejb name: dept

          6.             選擇是容器管理事務(CMP)還是bean自己管理事務(BMP),這里選擇CMP 2.x

          7.             選擇是遠程(remote)還是本地(local),還是兩者皆是(both),這里選擇遠程。

          8.             選擇Finish 完成操作,生成Dept.java:

          9.             添加自己的代碼:

          第一步 添加實體bean與數據庫表映射字段的讀寫方法

              /**

                 *@ejb.interface-methodview-type   ="remote"

                 *@ejb.persistence      column-name="deptNo"

                 */

              publicabstract Integer getDeptNo();

               /**

                 *@ejb.interface-methodview-type   ="remote"

                 */

              publicabstractvoid setDeptNo(Integer deptno);

             

               /**

                 *@ejb.interface-methodview-type   ="remote"

                 *@ejb.persistence      column-name="dName"

                 */

              publicabstract String getDName();

               

               /**

                 *@ejb.interface-methodview-type   ="remote"

                 */

              publicabstractvoid setDName(String name);

             

               /**

                 *@ejb.interface-methodview-type   ="remote"

                 *@ejb.persistence      column-name="loc"

                 */

              publicabstract String getLoc();

               /**

                 *@ejb.interface-methodview-type   ="remote"

                 */

              publicabstractvoid setLoc(String loc) ;

          注解1:在每個方法前加@ejb.interface-methodview-type   ="remote"的作用是在自動生成remote接口時生成相應的方法,如:

                public java.lang.Integer getDeptNo( )

                throwsjava.rmi.RemoteException;

                publicvoid setDeptNo( java.lang.Integer deptno )

                throwsjava.rmi.RemoteException;

                你也可以自己手動把這些方法加到remote接口里!

          注解2:在某個方法前加@ejb.persistence      column-name="loc"的作用是在自動生成weblogic-cmp-rdbms-jar.xmlejb-jar.xml兩個部署文件時生成如下紅色代碼部分,你也可以自己手寫紅色代碼部分。

          <weblogic-rdbms-jar>

             <weblogic-rdbms-bean>

                <ejb-name>Dept</ejb-name>

                <data-source-name></data-source-name>

                <table-name>Dept</table-name>

                <field-map> <!--bean屬性和數據庫列的映射-->

          <cmp-field>deptNo</cmp-field>

                   <dbms-column>deptNo</dbms-column>

                </field-map>

                <field-map>

                   <cmp-field>dName</cmp-field>

                   <dbms-column>dName</dbms-column>

                </field-map>

                <field-map>

                   <cmp-field>loc</cmp-field>

                   <dbms-column>loc</dbms-column>

                </field-map>

             </weblogic-rdbms-bean>

          <weblogic-rdbms-jar>

          <ejb-jar >

             <enterprise-beans>

                <entity >

                   <description><![CDATA[Description for Dept]]></description>

                   <display-name>Name for Dept</display-name>

                   <ejb-name>Dept</ejb-name>

                   <home>com.entity.cmp.interfaces.DeptHome</home>

                   <remote>com.entity.cmp.interfaces.Dept</remote>

                   <ejb-class>com.entity.cmp.ejb.Dept</ejb-class>

                   <persistence-type>Container</persistence-type>

                   <prim-key-class>com.entity.cmp.interfaces.DeptPK

          </prim-key-class>

                   <reentrant>False</reentrant>

                   <cmp-version>2.x</cmp-version>

                   <abstract-schema-name>Dept</abstract-schema-name>

                   <!--bean屬性定義,上面<cmp-field>和這里的<field-name>應一致-->

                   <cmp-field >

          <description><![CDATA[]]></description>

                      <field-name>deptNo</field-name>

                   </cmp-field>

                   <cmp-field >

                      <description><![CDATA[]]></description>

                      <field-name>dName</field-name>

                   </cmp-field>

                   <cmp-field >

                      <description><![CDATA[]]></description>

                      <field-name>loc</field-name>

                   </cmp-field>

                </entity>

          </ejb-jar >

          </enterprise-beans>

          第二步添加自己的 ejbCreate

          /**

               *用于實現數據持久化(插入數據庫)

               * @ejb.create-method

              **/

             public Integer ejbCreate(Integer deptid,String deptName,String deptLoc )

          {

                 setDeptNo(deptid);

                 setDName(deptName);

                 setLoc(deptLoc);

                 returnnull;

              }

          第三步添加相應的參數相同的ejbPostCreateejbCreate

              publicvoid ejbPostCreate(Integer deptid,String deptName,String deptLoc ){ }

          3.     XDoclet自動生成接口文件、部署描述文件

          3.1.  為工程配置XDoclet

          1.         右鍵單擊工程名字,打開工程的properties窗口。

          選擇菜單Properties > MyEclipse > XDoclet

          2.        build選項卡中選擇 Use dynamic build specification jdk1.4.2

          3.        單擊Add Standard,選擇Standard EJB

          4.        單擊Standard EJB,展開ejbdoclet樹菜單,取消dataobjectentitycmpentitypkvalueobject選項如下圖:

          5   為服務器配置XDoclet,該配置是為了自動生成weblogic-ejb-jar.xmlweblogic-cmp-rdbms-jar.xml文件。

          右擊ejbdoclet 選擇 Add

          我這里用的是weblogic8.1,所以選擇weblogic

          l         設置屬性destDir = src/META-INF (weblogic-ejb-jar.xmlweblogic-cmp-rdbms-jar.xml生成位置)

          l         設置屬性datasoruce =你在weblogic中配置的數據源jndi名稱。

          生成weblogic-cmp-rdbms-jar.xml中的:

          <data-source-name>aptechJNDI</data-source-name>

          5.3.  運行XDoclet生成文件

          在工程上右鍵MyEclipse->Run XDoclet

          生成前后工程目錄應該類似為:

          同時增加了weblogic-ejb-jar.xmlweblogic-cmp-rdbms-jar.xml,和ejb-jar.xml三個部署文件

          生成遠程接口,home接口和實用類。

          l         注意:DeptHome.javahome接口前有個紅色的叉,我們需要修改它,把com.entity.cmp.interfaces.DeptPK改成Integer,因為我們的bean的主鍵很簡單就是deptno,在數據庫中是個number(2)類型的,所以用Integer就可以了,而不用在定義一個com.entity.cmp.interfaces.DeptPK

          public com.entity.cmp.interfaces.Dept findByPrimaryKey(com.entity.cmp.interfaces.DeptPK pk)

                throws javax.ejb.FinderException,java.rmi.RemoteException;

          l         修改ejb-jar.xml

          1.       <entity >添加紅色代碼部分,指定某列為主鍵;把<prim-key-class>的值改成java.lang.Integer和主鍵的類型一致。

          <entity >

                   <description><![CDATA[Description for Dept]]></description>

                   <display-name>Name for Dept</display-name>

                   <ejb-name>Dept</ejb-name>

                   <home>com.entity.cmp.interfaces.DeptHome</home>

                   <remote>com.entity.cmp.interfaces.Dept</remote>

                   <ejb-class>com.entity.cmp.ejb.Dept</ejb-class>

                   <persistence-type>Container</persistence-type>

                   <prim-key-class>java.lang.Integer</prim-key-class>

                   <reentrant>False</reentrant>

                   <cmp-version>2.x</cmp-version>

                   <abstract-schema-name>Dept</abstract-schema-name>

                   <cmp-field >

                      <description><![CDATA[]]></description>

                      <field-name>deptNo</field-name>

                   </cmp-field>

                   <cmp-field >

                      <description><![CDATA[]]></description>

                      <field-name>dName</field-name>

                   </cmp-field>

                   <cmp-field >

                      <description><![CDATA[]]></description>

                      <field-name>loc</field-name>

                   </cmp-field>

                   <primkey-field>deptNo</primkey-field>

                </entity>

          2.<assembly-descriptor >中添加紅色部分,配置事務屬性

          <assembly-descriptor >

          <container-transaction>

                <method>

                  <ejb-name>Dept</ejb-name>

                  <method-name>*</method-name>

                </method>

                <trans-attribute>Required</trans-attribute>

              </container-transaction>

          </assembly-descriptor >

          5.4.  部署EJB

          1.        MyEclipse >Add and Remove Project Deployments

          2.        點擊add,添加部署服務器weblogic

          Entity Bean編寫到此結束

          編寫客戶端程序

          1.         新建一個java project

          2.         為工程配置classpath

          l         右鍵單擊該工程名 > build path > configer builder path > add class folder 選擇

          上面創建的ejb工程的classes目錄點擊ok

          右擊上面創建的ejb工程的src目錄選擇export(導出)> 選擇jar file >如下圖單擊select the export destination 欄目的browse選擇存儲地址。然后右鍵單擊該工程名 > build path > configer builder path > add external jars 選擇你剛導出的jar文件,加入到classpath

          l         然后右鍵單擊該工程名 > build path > configer builder path > add external jars 選擇D:"Program Files"bea"weblogic81"server"lib"weblogic.jar,加入到java class path

          3.         創建一個classClient.java

          publicclass Client {

              protected String serverUrl;

              protected String jndiName;

              protected DeptHome home;

              /**

               *構造方法:根據服務器地址端口和ejb部署的jndiname初始化類域,獲得EJBHome

               *@paramurl

               *@paramjndiName

               *@throwsNamingException

               */

              public Client(String url, String jndiName) throws NamingException {

                 this.jndiName = jndiName;

                 this.serverUrl = url;

                 home = lookupHome();

              }

              /**

               *獲得初始化上下文環境(javax.naming.Context)實例從服務器JNDItree.

               */

              private Context getInitialContext() throws NamingException {

                 try {

                     // Get an InitialContext

                     Hashtable h = new Hashtable();

                     h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

                     h.put(Context.PROVIDER_URL, serverUrl);

                     returnnew InitialContext(h);

                 } catch (NamingException ne) {

                     log("我們無法通過該地址:"

                            + serverUrl

                            + "獲得一個到 WebLogic server 的連接!/"

                            + "We were unable to get a connection to the WebLogic server at "

                            + serverUrl);

                     log("請確保該服務器是啟動的。/Please make sure that the server is running.");

                     throw ne;

                 }

              }

              /**

               *通過JNDI服務查找beanhome接口,并返回/Lookupthebean'shomeinterfaceusingJNDI.

               */

              private DeptHome lookupHome() throws NamingException {

                 Context ctx = getInitialContext();

                 try

                 {

                     Object home = (DeptHome) ctx.lookup(jndiName);

                     return (DeptHome) PortableRemoteObject.narrow(home, DeptHome.class);

                 } catch (NamingException ne) {

                      log("客戶端無法獲得EJBHome"

                            + "請確保你已經用"""+ jndiName+"""這個JNDI name"

                            + serverUrl

                            + "WebLogic server上部署了此ejb");

                      log("The client was unable to lookup the EJBHome. Please make sure " +

                              "that you have deployed the ejb with the JNDI name " +

                              "ejb/DeptHome on the WebLogic server at "+serverUrl);

                     throw ne;

                 }

              }

             

             

              /**

               *打印日志

               *

               *@params

               */

              privatestaticvoid log(String s) {

                 System.out.println(s);

              }

             

             

             

              publicstaticvoid main (String args[])

              {

                 log("開始客戶端測試:");

                

                 String url = "t3://localhost:7001";

                 String jndi = "ejb/Dept"// 來自weblogic-ejb-jar.xml<jndi-name>ejb/Dept</jndi-name>

                 Client client = null;

                  // 如果給了main參數則用main方法的參數

                 if(args.length==1)

                 {          

                    url = args[0];

                 }

                

                

                 try

                 {

                    

                     client = new Client(url,jndi);

                     DeptHome home = client.home;

                     log("獲得DeptHome");

                     PortableRemoteObject.narrow(home.create(new Integer(1),"java","zhengzhou"), Dept.class);

                     log("成功創建編號為1的部門!");

                     log("開始執行home.findByPrimaryKey根據主鍵(部門編號)1執行部門查詢!");

                     Dept dept;

                     dept = (Dept) PortableRemoteObject.narrow(home.findByPrimaryKey(new Integer(1)),Dept.class);

                     Integer no = dept.getDeptNo();

                     String name = dept.getDName();

                     String loc = dept.getLoc();

                     log("部門編號"t部門名字"t位置");

                     log(no +""t"+name +""t"+loc);

                    

                     log("game over");

                 } catch (NamingException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                 } catch (ClassCastException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                 } catch (RemoteException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                 } catch (CreateException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                 } catch (FinderException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                 }

                

              }

          posted on 2008-04-08 19:13 whistler 閱讀(2871) 評論(5)  編輯  收藏


          FeedBack:
          # re: MyEclipse開發EJB之CMP實體bean[未登錄]
          2008-07-02 17:46 | 張樹坤
          非常感謝您的這篇文章,最近手頭上有一個EJB2.0的項目,需要用到,所以一直在找相關資料,這篇寫的相當的詳細,謝謝。
          另外我還想多問一句?

          如何在CMP Bean中定義自己想要實現的業務方法呢。
          比如我想查詢所有的記錄SelectAllDept()方法,應該怎么寫
          是否要定義@ejb.finder
          等。
          期待回復。

            回復  更多評論
            
          # re: MyEclipse開發EJB之CMP實體bean
          2008-11-27 16:45 | fengda2870
          網上關于EJB的教程比較少
          找了好久才找到
          非常感謝您的這篇文章
            回復  更多評論
            
          # re: MyEclipse開發EJB之CMP實體bean[未登錄]
          2009-03-29 02:09 | 111
          PortableRemoteObject 從哪來的

          InitialContext 從哪來的


          能不能寫清楚點。


          不過還是謝謝了。  回復  更多評論
            
          # re: MyEclipse開發EJB之CMP實體bean
          2009-03-29 12:28 | 222
          開始客戶端測試:
          javax.naming.CommunicationException [Root exception is java.net.ConnectException: t3://localhost:7001: Destination unreachable; nested exception is:
          java.net.ConnectException: Connection refused: connect; No available router to destination]
          at weblogic.jndi.internal.ExceptionTranslator.toNamingException(ExceptionTranslator.java:47)
          at weblogic.jndi.WLInitialContextFactoryDelegate.toNamingException(WLInitialContextFactoryDelegate.java:651)
          at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:320)
          at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:253)
          at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:135)
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
          at javax.naming.InitialContext.init(Unknown Source)
          at javax.naming.InitialContext.<init>(Unknown Source)
          at Client.getInitialContext(Client.java:38)
          at Client.lookupHome(Client.java:62)
          at Client.<init>(Client.java:29)
          at Client.main(Client.java:110)
          Caused by: java.net.ConnectException: t3://localhost:7001: Destination unreachable; nested exception is:
          java.net.ConnectException: Connection refused: connect; No available router to destination
          at weblogic.rjvm.RJVMFinder.findOrCreate(RJVMFinder.java:200)
          at weblogic.rjvm.ServerURL.findOrCreateRJVM(ServerURL.java:125)
          at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:310)
          ... 10 more


          classpath里面也加了weblogic.jar的路徑

          以上問題運行之后出現的,請問該如何解決。  回復  更多評論
            
          # re: MyEclipse開發EJB之CMP實體bean
          2009-11-10 10:31 | landor
          非常好,照著例子,一步一步的做,再參照
          http://xdoclet.sourceforge.net/xdoclet/tags/ejb-tags.html#@ejb_persistence__0__1_的說明,很快做了個例子,哈哈  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(2)

          我參與的團隊

          文章檔案(22)

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 金秀| 兴文县| 桃江县| 南阳市| 邵阳市| 衡山县| 和静县| 丹凤县| 新巴尔虎左旗| 隆林| 怀仁县| 靖边县| 亳州市| 稷山县| 紫阳县| 临朐县| 崇明县| 黑水县| 通榆县| 景泰县| 寿宁县| 新宾| 平湖市| 靖边县| 乌拉特中旗| 太白县| 蛟河市| 上犹县| 城步| 玛纳斯县| 宿州市| 木兰县| 黎平县| 包头市| 塘沽区| 通辽市| 镇巴县| 永州市| 平远县| 龙泉市| 福建省|