posts - 30,  comments - 85,  trackbacks - 0
          JDO 的使用和集成
               這里不想討論JDO和JPA的區別,也不討論JDO的規范,單只是從JDO的使用和與應用的集成角度來概述。

          1. 下載JDO規范的實現開源包。目前主流的JDO實現有:
          *.TJDO http://tjdo.sf.net
          *.Speedo http://speedo.objectweb.org
          *.JORM http://jorm.objectweb.org
          *.XORM http://xorm.sourceforge.net
          *.JPOX http://jpox.sourceforge.net
          *.OJB http://db.apache.org/ojb/
          *.DataNucleus http://www.datanucleus.org/
           
          2. 這里選擇DataNucleus為JDO的實現,因為個人認為datanucleus大有一統O/R Mapping天下的架勢。前端支持JDO/JPA規范,后面能接多種數據存儲平臺(RDBMS, ODBMS, Map-based, Web-based, documents, etc) . 并且直接可與bigtable、hbase等分布式數據庫集成。實在強大。
               * 下載DataNucleus程序包
               * 下載DataNucleus在eclipse下的擴展插件

          3. 如果使用dataNucleus連接DBMS數據庫(Mysql)需要加入以下幾個jar包:
               datanucleus-enhancer-3.0.0-m4.jar   jdo-api-3.1-SNAPSHOT-20110319.jar  datanucleus-api-jdo-3.0.0-m4.jar  datanucleus-jdo-query-3.0.0-m2.jar    asm.jar
               mysql-connector-java-5.0.4-bin.jar  datanucleus-cache-3.0.0-m2.jar    datanucleus-management-1.0.2.jar datanucleus-core-3.0.0-m4.jar     datanucleus-rdbms-3.0.0-m4.jar

          4. 創建一個entity class 并配置映射文件 如下:
               entity class :
          @PersistenceCapable
          public class Person {     
                 @PrimaryKey
                 private String name ;
                 private int age ;
                 private String mail ;
               .......
          }
             mapping xml
          <jdo>
              <package name="com.jdo.data.nucleus.model">
                  <class name="Person">
                      <field name="name" persistence-modifier="persistent">
                           <column length="100" />
                      </field>
                      <field name="age" persistence-modifier="persistent"/>
                      <field name="mail" persistence-modifier="persistent"/>
                  </class>
              </package>
          </jdo>

          5.創建JDO操作類:
                       Map<String,String> JDOConfig = new HashMap<String,String>();
                      JDOConfig.put( "javax.jdo.PersistenceManagerFactoryClass" ,"org.datanucleus.api.jdo.JDOPersistenceManagerFactory" );
                     
                      JDOConfig.put("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost/acegi" );
                      JDOConfig.put( "javax.jdo.option.ConnectionDriverName" , "com.mysql.jdbc.Driver" );
                      JDOConfig.put( "javax.jdo.option.ConnectionUserName" , "root");
                      JDOConfig.put( "javax.jdo.option.ConnectionPassword" , "root");
                      JDOConfig.put( "javax.jdo.option.NontransactionalRead" , "true");
                      JDOConfig.put( "javax.jdo.option.NontransactionalWrite" , "true");
                      JDOConfig.put( "datanucleus.autoCreateSchema" , "true" );
                     
                      PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(JDOConfig);
                      PersistenceManager pm = pmf.getPersistenceManager();
                      Person person = null;
                      System. out.println("Insert iterm into DB. " );
                       //insert
                      person = new Person("wenhao" ,123,"wenhao@gmail.com");
                      pm.makePersistent(person);
                       //select
                       getPersonsFromDB(pm);
                       //update
                      person.setMail( "wenhao@sina.com.cn");
                      pm.close();
                     
                      person.setAge(1000);
                      System. out.println("instance level : " + person.getAge());
                     
                      pmf = JDOHelper. getPersistenceManagerFactory(JDOConfig);
                      pm = pmf.getPersistenceManager();
                      List<Person> updatePersons = getPersonsFromDB(pm);
                       if(updatePersons != null && updatePersons.size() > 0) {
                             for(Person updatePerson : updatePersons)
                                   //delete
                                  pm.deletePersistent(updatePerson);
                            System. out.println("Delete iterms from DB." );
                      }
                      pm.close();
                      //select
                      Query q = pm.newQuery( "SELECT FROM " + Person.class.getName() + " WHERE name == 'wenhao'" );
                      List<Person> updatePersons = (List)q.execute();

          6. 運行JDO操作類,這時直接運行會報如下的錯誤:
          This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data/annotations for the class are not found
               這個錯誤的是因為沒有對Entity class進行jdo的enhance來增強entity的二進制代碼。故需要在project上增加datanucleus支持。并運行enhancer tool來enhance entity。運行后可通過反編譯工具來查看transform后的class。可發現多了一些屬性和方法。這些都是對JDO entity的擴展。

          7.正常運行
               會發現在person.setMail( "wenhao@sina.com.cn" );會更改到底層的數據庫中,而person.setAge(1000);并不會更改數據庫字段。其實很容易理解。類似hibernate的entity的幾個狀態,自由態·持久態·游離態。 當pm.close();后person處于自由態,修改的屬性只有在instance內部有效。而在pm cloase或transaction.commit之前時,entity仍處理持久化狀態,直接修改屬性,會持久化到底層數據庫中。
              
          8.JDO對象的多對多關系實現。多對多和hibernate的配置類似,主要還是在配置文件上做一些配置。配置文件如下:
          Person.jdo:
          <jdo>
              <package name="com.jdo.data.nucleus.model">
                  <class name="Person">
                      <field name="name" persistence-modifier="persistent">
                            <column length="100" jdbc-type="VARCHAR" />
                      </field>
                      <field name="age" persistence-modifier="persistent"/>
                      <field name="mail" persistence-modifier="persistent"/>
                      <field name="roles" persistence-modifier="persistent" table="PERSON_RULE">
                            <collection element-type="com.jdo.data.nucleus.model.Role" />
                            <join>
                              <column name="name" />
                          </join>
                          <element>
                              <column name="roleName" />
                          </element>
                      </field>

                  </class>
              </package>
          </jdo>
          Role.jdo:
          <jdo>
              <package name="com.jdo.data.nucleus.model">
                  <class name="Role">
                      <field name="roleName" persistence-modifier="persistent">
                            <column length="100" jdbc-type="VARCHAR" />
                      </field>
                      <field name="roleDesc" persistence-modifier="persistent"/>
                      <field name="persons" persistence-modifier="persistent" table="PERSON_RULE">
                            <collection element-type="com.jdo.data.nucleus.model.Person" />
                            <join>
                              <column name="roleName" />
                          </join>
                          <element>
                              <column name="name" />
                          </element>
                      </field>

                  </class>
              </package>
          </jdo>
          Person.java:
          @PersistenceCapable
          public class Person {
                 @PrimaryKey
                 private String name ;
                 private int age ;
                 private String mail ;     
                 @Element
                 private Set<Role> roles ;
          }
          Role.java:
          @PersistenceCapable
          public class Role {
                 @PrimaryKey
                 private String roleName ;
                 private String roleDesc ;
                 @Element
                 private Set<Person> persons ;
          }
          以上配置可實現多對多的關系。并能夠進行關聯查詢。

          posted on 2011-05-15 17:19 安文豪 閱讀(2840) 評論(0)  編輯  收藏

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


          網站導航:
           

          <2011年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(6)

          隨筆檔案(28)

          文章分類(3)

          文章檔案(4)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 86800
          • 排名 - 670

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青神县| 兰西县| 西安市| 阿克| 疏勒县| 尼勒克县| 贺兰县| 乐安县| 博乐市| 新邵县| 青龙| 九江县| 平和县| 彭阳县| 普定县| 仁布县| 林芝县| 云林县| 临潭县| 阳原县| 汉中市| 华坪县| 腾冲县| 麻栗坡县| 老河口市| 山丹县| 深圳市| 通化县| 建水县| 阿拉善左旗| 江津市| 鄂尔多斯市| 锦屏县| 海门市| 建水县| 泾阳县| 伊金霍洛旗| 庆元县| 南宁市| 炉霍县| 福海县|