DataNucleus提供了一些示例代码,http://sourceforge.net/projects/datanucleus/files/datanucleus-samples/
其中有 datanucleus-samples-jpa-tutorial-3.2-src.zip 和 datanucleus-samples-jdo-tutorial-3.2-src.zip
解压这两个文件,可以得到JPA与JDO访问的示例代码。
关于这两个示例的介绍在http://www.datanucleus.org/products/accessplatform_2_1/guides/jpa/tutorial.html , http://www.datanucleus.org/products/accessplatform_2_1/guides/jdo/tutorial.html
对于RDBMS, 原代码中使用的是hsqldb,这里改为mysql,具体修改如下:
JPA访问
=============================================
META-INF/persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- Tutorial "unit" -->
<persistence-unit name="Tutorial">
<mapping-file>org/datanucleus/samples/jpa/tutorial/orm.xml</mapping-file>
<class>org.datanucleus.samples.jpa.tutorial.Inventory</class>
<class>org.datanucleus.samples.jpa.tutorial.Product</class>
<class>org.datanucleus.samples.jpa.tutorial.Book</class>
<properties>
<!-- ENABLE THESE FOR RDBMS -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="las"/>
<property name="javax.persistence.jdbc.password" value="las"/>
<property name="datanucleus.autoCreateSchema" value="true"/>
</properties>
</persistence-unit>
</persistence>
pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
编译运行过程:
1. Run the command: "mvn clean compile"
This builds everything, and enhances the classes
2. Run the command: "mvn datanucleus:schema-create"
This creates the schema for this sample.
3. Run the command: "mvn exec:java"
This runs the tutorial
4. Run the command: "mvn datanucleus:schema-delete"
This deletes the schema for this sample.
JDO访问
======================================================
datanucleus.properties:
javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
javax.jdo.option.ConnectionURL=jdbc:mysql://127.0.0.1/jpa?useServerPrepStmts=false
javax.jdo.option.ConnectionUserName=las
javax.jdo.option.ConnectionPassword=las
javax.jdo.option.Mapping=mysql
datanucleus.metadata.validate=false
datanucleus.autoCreateSchema=true
datanucleus.validateTables=false
datanucleus.validateConstraints=false
pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
编译运行过程:
1. Run the command: "mvn clean compile"
This builds everything, and enhances the classes
2. Run the command: "mvn datanucleus:schema-create"
This creates the schema for this sample.
3. Run the command: "mvn exec:java"
This runs the tutorial
4. Run the command: "mvn datanucleus:schema-delete"
This deletes the schema for this sample.
整理后的maven工程在:http://download.csdn.net/detail/sundongsdu/4978335