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

          1. 下載JDO規(guī)范的實現(xiàn)開源包。目前主流的JDO實現(xiàn)有:
          *.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的實現(xiàn),因為個人認為datanucleus大有一統(tǒng)O/R Mapping天下的架勢。前端支持JDO/JPA規(guī)范,后面能接多種數(shù)據(jù)存儲平臺(RDBMS, ODBMS, Map-based, Web-based, documents, etc) . 并且直接可與bigtable、hbase等分布式數(shù)據(jù)庫集成。實在強大。
               * 下載DataNucleus程序包
               * 下載DataNucleus在eclipse下的擴展插件

          3. 如果使用dataNucleus連接DBMS數(shù)據(jù)庫(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. 創(chuàng)建一個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.創(chuàng)建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??砂l(fā)現(xiàn)多了一些屬性和方法。這些都是對JDO entity的擴展。

          7.正常運行
               會發(fā)現(xiàn)在person.setMail( "wenhao@sina.com.cn" );會更改到底層的數(shù)據(jù)庫中,而person.setAge(1000);并不會更改數(shù)據(jù)庫字段。其實很容易理解。類似hibernate的entity的幾個狀態(tài),自由態(tài)·持久態(tài)·游離態(tài)。 當pm.close();后person處于自由態(tài),修改的屬性只有在instance內(nèi)部有效。而在pm cloase或transaction.commit之前時,entity仍處理持久化狀態(tài),直接修改屬性,會持久化到底層數(shù)據(jù)庫中。
              
          8.JDO對象的多對多關(guān)系實現(xiàn)。多對多和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 ;
          }
          以上配置可實現(xiàn)多對多的關(guān)系。并能夠進行關(guān)聯(lián)查詢。

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

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


          網(wǎng)站導(dǎo)航:
           

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

          常用鏈接

          留言簿(6)

          隨筆檔案(28)

          文章分類(3)

          文章檔案(4)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 86800
          • 排名 - 670

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新安县| 泰兴市| 仙桃市| 阿勒泰市| 堆龙德庆县| 新营市| 武乡县| 横山县| 漳州市| 和政县| 盐城市| 青阳县| 双峰县| 英山县| 韶关市| 大英县| 红安县| 大足县| 筠连县| 团风县| 宜春市| 历史| 都江堰市| 南丹县| 五莲县| 江城| 德钦县| 项城市| 罗城| 水城县| 富阳市| 聂荣县| 鞍山市| 莱西市| 林西县| 宣城市| 宜黄县| 绵竹市| 永宁县| 沽源县| 肃北|