JDO 的使用和集成 – JAVA編程語言程序開發技術文章

這裡不想討論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 v

發佈留言