JPOX:免费的JDO实现(1.2版本开始支持JPA1)

来源:互联网 发布:查看opengl版本 linux 编辑:程序博客网 时间:2024/05/22 10:05
官方主http://www.jpox.org/

http://www.jpox.org/docs/download.html

另外需要JDO,可以使用sun实现,也可以使用apache实现(http://db.apache.org/jdo/downloads.html)

 

测试步骤如下:

MySQL中新建数据jpox用。

工程布局如下

 

jpox.properties:

  1. javax.jdo.PersistenceManagerFactoryClass=org.jpox.jdo.JDOPersistenceManagerFactory
  2. javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
  3. javax.jdo.option.ConnectionURL=jdbc:mysql://localhost:3306/jpox
  4. javax.jdo.option.ConnectionUserName=root
  5. javax.jdo.option.ConnectionPassword=root
  6. org.jpox.autoCreateSchema=true
  7. org.jpox.validateTables=false
  8. org.jpox.validateConstraints=false

log4j.properties

  1. # LOG4J Configuration
  2. # ===================
  3. # Basic logging goes to "jpox.log"
  4. log4j.appender.A1=org.apache.log4j.FileAppender
  5. log4j.appender.A1.File=jpox.log
  6. log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} (%t) %-5p [%c] - %m%n
  8. #log4j.appender.A1.Threshold=INFO
  9. # Categories
  10. # Each category can be set to a "level", and to direct to an appender
  11. # Default to DEBUG level for all JPOX categories
  12. log4j.logger.JPOX = DEBUG, A1
  13. #log4j.category.JPOX.JDO=DEBUG, A1
  14. #log4j.category.JPOX.JPA=DEBUG, A1
  15. #log4j.category.JPOX.Persistence=DEBUG, A1
  16. #log4j.category.JPOX.Lifecycle=DEBUG, A1
  17. #log4j.category.JPOX.Query=DEBUG, A1
  18. #log4j.category.JPOX.Cache=DEBUG, A1
  19. #log4j.category.JPOX.Reachability=DEBUG, A1
  20. #log4j.category.JPOX.MetaData=DEBUG, A1
  21. #log4j.category.JPOX.General=DEBUG, A1
  22. #log4j.category.JPOX.Utility=DEBUG, A1
  23. #log4j.category.JPOX.Transaction=DEBUG, A1
  24. #log4j.category.JPOX.Store.Poid=DEBUG, A1
  25. #log4j.category.JPOX.Naming=DEBUG, A1
  26. #log4j.category.JPOX.Management=DEBUG, A1
  27. #log4j.category.JPOX.Datastore=DEBUG, A1
  28. #log4j.category.JPOX.Connection=DEBUG, A1
  29. #log4j.category.JPOX.ClassLoading=DEBUG, A1
  30. #log4j.category.JPOX.Plugin=DEBUG, A1
  31. #log4j.category.JPOX.Enhancer=DEBUG, A1
  32. #log4j.category.JPOX.SchemaTool=DEBUG, A1
  33. #log4j.category.JPOX.TEST=DEBUG, A1
  34. #
  35. # C3P0 logging
  36. #
  37. #log4j.category.com.mchange.v2.c3p0=INFO, A1
  38. #log4j.category.com.mchange.v2.resourcepool=INFO, A1
  39. #
  40. # Proxool logging
  41. #
  42. #log4j.category.org.logicalcobwebs.proxool=INFO,A1

Author.java

  1. package examples.jdo2.model;
  2. public class Author {
  3.     
  4.     private int books;
  5.     private String name;
  6.     public Author(String name, int books) {
  7.         this.name = name;
  8.         this.books = books;
  9.     }
  10.     protected Author() {
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.     public int getBooks() {
  19.         return books;
  20.     }
  21.     public void setBooks(int books) {
  22.         this.books = books;
  23.     }
  24. }

Author.jdo

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE jdo PUBLIC
  3.     "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN"
  4.     "http://java.sun.com/dtd/jdo_2_0.dtd">
  5. <jdo>
  6.    <package name="examples.jdo2.model">
  7.        <class name="Author" identity-type="datastore">
  8.            <field name="books" persistence-modifier="persistent"></field>
  9.            <field name="name" persistence-modifier="persistent">
  10.                 <column length="50" jdbc-type="VARCHAR"/>
  11.            </field>
  12.        </class>
  13.    </package>
  14. </jdo>

MakePersistent.java

  1. package examples.jdo2;
  2. import javax.jdo.JDOHelper;
  3. import javax.jdo.PersistenceManager;
  4. import javax.jdo.PersistenceManagerFactory;
  5. import javax.jdo.Transaction;
  6. import examples.jdo2.model.Author;
  7. public class MakePersistent {
  8.     
  9.     public static void main(String[] args) {
  10.         
  11.         PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("jpox.properties");
  12.         PersistenceManager pm = pmf.getPersistenceManager();
  13.         Transaction tx = pm.currentTransaction();
  14.         tx.begin();
  15.         Author au = new Author("Chen Yan", 5);
  16.         System.out.println("Author: " + au.getName() + "/t" + au.getBooks() + " Books");
  17.         pm.makePersistent(au);
  18.         tx.commit();
  19.         // tx.rollback();
  20.         // Can not read fields outside of transactions. Or set:
  21.         // pmf.setNontransactionalRead(true);
  22.         // System.out.println("Author: " + au.getName() + "/t" + au.getBooks() + " Books");
  23.         tx.begin();
  24.         String name = au.getName();
  25.         System.out.println("Author: " + name);
  26.         tx.commit();
  27.         pm.close();
  28.         pmf.close();
  29.     }
  30. }

ReadExtent.java

  1. package examples.jdo2;
  2. import java.util.Iterator;
  3. import javax.jdo.Extent;
  4. import javax.jdo.JDOHelper;
  5. import javax.jdo.PersistenceManager;
  6. import javax.jdo.PersistenceManagerFactory;
  7. import javax.jdo.Transaction;
  8. import examples.jdo2.model.Author;
  9. public class ReadExtent {
  10.     public static void main(String[] args) {
  11.         
  12.         PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("jpox.properties");
  13.         PersistenceManager pm = pmf.getPersistenceManager();
  14.         Transaction tx = pm.currentTransaction();
  15.         tx.begin();
  16.         Extent extent = pm.getExtent(Author.classfalse);
  17.         Iterator itor = extent.iterator();
  18.         Author au;
  19.         while (itor.hasNext()) {
  20.             au = (Author) itor.next();
  21.             System.out.println("Author: " + au.getName() + " |/t"
  22.             + au.getBooks());
  23.         }
  24.         extent.close(itor);
  25.         tx.commit();
  26.         pm.close();
  27.         pmf.close();
  28.     }
  29. }

ReadQuery.java

  1. package examples.jdo2;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import javax.jdo.JDOHelper;
  5. import javax.jdo.PersistenceManager;
  6. import javax.jdo.PersistenceManagerFactory;
  7. import javax.jdo.Query;
  8. import javax.jdo.Transaction;
  9. import examples.jdo2.model.Author;
  10. public class ReadQuery {
  11.     public static void main(String[] args) {
  12.         PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("jpox.properties");
  13.         PersistenceManager pm = pmf.getPersistenceManager();
  14.         Transaction tx = pm.currentTransaction();
  15.         tx.begin();
  16.         Query query = pm.newQuery(Author.class"books == 7");
  17.         Collection result = (Collection) query.execute();
  18.         Iterator itor = result.iterator();
  19.         Author au;
  20.         while (itor.hasNext()) {
  21.             au = (Author) itor.next();
  22.             System.out.println("Author: " + au.getName() + " |/t" + au.getBooks());
  23.         }
  24.         query.close(result);
  25.         tx.commit();
  26.         pm.close();
  27.         pmf.close();
  28.     }
  29. }

因为在JDO需要Enhance以得到的PO,而且只能静态编译,不能运行期编译。这里使用ant脚本。

build.xml

  1. <!--
  2. ===================================================================
  3. tutorial build
  4. ===================================================================
  5. -->
  6. <project name="jdo2jpox" default="compile">
  7.     <!-- environment -->
  8.     <property environment="env"/>
  9.     <property file="jpox.properties"/>
  10.     <property name="project.location" location="."/>
  11.     <property name="project.build.debug" value="on"/>
  12.     <property name="Name" value="tutorial"/>
  13.     <property name="name" value="${Name}"/>
  14.     <property name="version" value="1.2"/>
  15.     <!-- project workspace directories -->
  16.     <property name="java.dir" value="src"/>
  17.     <property name="lib.dir" value="."/>
  18.     <!-- compile properties -->
  19.     <property name="classes.dir" value="bin"/>
  20.     <!--
  21.     ===================================================================
  22.     Classpath properties
  23.     ===================================================================
  24.     -->
  25.     <!-- the classpath for running -->
  26.     <path id="lib.classpath">
  27.         <fileset dir="${lib.dir}">
  28.             <include name="**/*.jar"/>
  29.         </fileset>
  30.         <pathelement location="${classes.dir}"/>
  31.         <pathelement location="${basedir}"/>
  32.     </path>
  33.     <!-- the classpath for the compile -->
  34.     <path id="compile.classpath">
  35.         <pathelement location="${classes.dir}"/>
  36.         <path refid="lib.classpath"/>
  37.     </path>
  38.     <!--
  39.     ===================================================================
  40.     TARGET : clean
  41.     ===================================================================
  42.     -->
  43.     <target name="clean">
  44.         <delete includeEmptyDirs="true" quiet="true">
  45.             <fileset dir="${classes.dir}" includes="**/*.class,**/*.properties,**/*.*"/>
  46.         </delete>
  47.     </target>
  48.     <!--
  49.     ===================================================================
  50.     TARGET : prepare
  51.     ===================================================================
  52.     -->
  53.     <target name="prepare">
  54.         <mkdir dir="${classes.dir}"/>
  55.     </target>
  56.     <!--
  57.     ===================================================================
  58.     TARGET : compile.java
  59.     ===================================================================
  60.     -->
  61.     <target name="compile" depends="clean,prepare">
  62.         <echo message="==================================================================="/>
  63.         <echo message="Compile configuration:"/>
  64.         <echo message="java.dir          = ${java.dir}"/>
  65.         <echo message="classes.dir       = ${classes.dir}"/>
  66.         <echo message="basedir           = ${basedir}"/>
  67.         <echo message="==================================================================="/>
  68.         <javac srcdir="${java.dir}" destdir="${classes.dir}" debug="${project.build.debug}" classpathref="compile.classpath">
  69.             <include name="**/*.java"/>
  70.         </javac>
  71.     </target>
  72.     <!--
  73.     ===================================================================
  74.     TARGET : copy jdo metadata files
  75.     ===================================================================
  76.     -->
  77.     <target name="copy.metadata">
  78.         <copy todir="${classes.dir}">
  79.             <fileset dir="${java.dir}" includes="**/*.jdo"/>
  80.         </copy>
  81.     </target>
  82.     <!--
  83.     ===================================================================
  84.     TARGET : enhance
  85.     ===================================================================
  86.     -->
  87.     <target name="enhance" depends="compile,copy.metadata">
  88.         <!-- define the task enhancer -->
  89.         <taskdef name="enhancer" classname="org.jpox.enhancer.tools.EnhancerTask">
  90.             <classpath refid="compile.classpath"/>
  91.         </taskdef>
  92.         <!-- enhance -->
  93.         <enhancer classpathref="compile.classpath"
  94.             dir="${classes.dir}"
  95.             verbose="true">
  96.             <sysproperty key="log4j.configuration" value="file:log4j.properties"/>
  97.         </enhancer>
  98.     </target>
  99.     <!-- SchemaTool "create" -->
  100.     <target name="createschema">
  101.         <taskdef name="schematool" classname="org.jpox.SchemaToolTask">
  102.             <classpath refid="compile.classpath"/>
  103.         </taskdef>
  104.         <schematool classpathref="compile.classpath"
  105.             failonerror="true" verbose="true" mode="create" props="jpox.properties">
  106.             <fileset dir="${basedir}/bin">
  107.                 <include name="**/*.jdo"/>
  108.             </fileset>
  109.             <sysproperty key="log4j.configuration" value="${basedir}/log4j.properties"/>
  110.         </schematool>
  111.     </target>
  112.     <!-- SchemaTool "delete" -->
  113.     <target name="deleteschema">
  114.         <taskdef name="schematool" classname="org.jpox.SchemaToolTask">
  115.             <classpath refid="compile.classpath"/>
  116.         </taskdef>
  117.         <schematool classpathref="compile.classpath"
  118.             failonerror="true" fork="true" verbose="true" mode="delete" props="jpox.properties">
  119.             <fileset dir="${basedir}/bin">
  120.                 <include name="**/*.jdo"/>
  121.             </fileset>
  122.             <sysproperty key="log4j.configuration" value="log4j.properties"/>
  123.         </schematool>
  124.     </target>
  125.     <!-- SchemaTool "dbinfo" -->
  126.     <target name="schemainfo">
  127.         <taskdef name="schematool" classname="org.jpox.SchemaToolTask">
  128.             <classpath refid="compile.classpath"/>
  129.         </taskdef>
  130.         <schematool classpathref="compile.classpath"
  131.             failonerror="true" fork="true" verbose="true" mode="dbinfo" props="jpox.properties">
  132.             <fileset dir="${basedir}/bin">
  133.                 <include name="**/*.jdo"/>
  134.             </fileset>
  135.             <sysproperty key="log4j.configuration" value="log4j.properties"/>
  136.         </schematool>
  137.     </target>
  138.     <!-- Run the Tutorial -->
  139.     <target name="runtutorial" description="Run the application">
  140.         <java classname="examples.jdo2.MakePersistent" classpathref="lib.classpath" fork="true"/>
  141.         <java classname="examples.jdo2.ReadExtent" classpathref="lib.classpath" fork="true"/>
  142.         <java classname="examples.jdo2.ReadQuery" classpathref="lib.classpath" fork="true"/>
  143.     </target>
  144. </project>

先进行enhance后再进行runtutorial操作即可。

这时数据库中将直接生成数据库并进行数据操作。

注意:JDO在运行时会自动生成jpox_tables和sequence_table这2张表。

 

当然我们还可以使用persistence.xml文件配置数据库连接:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
  5.         http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  6.     <persistence-unit name="JPoxStore">
  7.         <provider>org.jpox.jpa.PersistenceProviderImpl</provider>
  8.         <class>examples.jdo2.model.Author</class>
  9.         <properties>
  10.             <property name="javax.jdo.option.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
  11.             <property name="javax.jdo.option.ConnectionURL" value="jdbc:mysql://localhost:3306/jpox"/>
  12.             <property name="javax.jdo.option.ConnectionUserName" value="root"/>
  13.             <property name="javax.jdo.option.ConnectionPassword" value="root"/>
  14.         </properties>
  15.     </persistence-unit>
  16. </persistence>

调用的方法是:

  Properties props = new Properties();
  props.put("javax.jdo.option.PersistenceUnitName", "JPoxStore");
  PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);

 

参考:http://www.jpox.org/docs/1_2/persistence_unit.html

 

本例参考自:http://blog.csdn.net/e_ville/archive/2006/12/27/1464180.aspx

关于Hibernate和JDO的比较:http://www.bitscn.com/mysql/JSPMySQL/200701/93732.html

其他资料:http://blog.csdn.net/liumm1983/archive/2007/03/19/1533726.aspx

 

原创粉丝点击