OpenJPA Sample Code
The OpenJPA distribution comes with a number of examples that illustrate the
usage of various features.
JDO - JPA Persistence Interoperability
This sample demonstrates how to combine JDO and JPA in a single application. The
MachineMain.java program uses both
EntityManagers and PersistenceManagers in a
single transaction including persist, delete and query operations.
The sample includes both annotated persistent classes as well as JDOR metadata
information. The application can switch to either system simply by changing the
bootstrap mechanism. Depending on which configuration system you use, OpenJPA
will read the corresponding metadata format. You can override some or all of
this behavior using OpenJPA's configuration options, such as
openjpa.MetaDataFactory.
To use this sample, you should ensure that either a jdo.properties
or persistence.xml are in the
META-INF directory in your CLASSPATH. The rest of the
files for this sample are located in the samples/mixed
directory of the OpenJPA installation. This tutorial requires JDK 5. To run this
tutorial:
Ensure that your environment is set properly as described in the README and that
your current path is in the mixed sample directory.
You may want to edit ConnectionURL to point to an absolute
URL (e.g. C:/openjpa/mixed-sample-db) if using a
file-based database like HSQL.
Include the list of persistent classes in your configuration file. For JPA, you
will want to add the following lines to persistence.xml
before the <property> lines:
<class>samples.mixed.Machine</class>
<class>samples.mixed.Crane</class>
<class>samples.mixed.Bulldozer</class>
<class>samples.mixed.Operator</class>
If you are using JDO, point the metadata factory at the .jdo
resource containing your persistent classes:
openjpa.MetaDataFactory: Resources=samples/mixed/package.jdo
Compile the classes:
javac *.java
You should then proceed to pass in the configuration file you are using to the
enhancer:
java org.apache.openjpa.enhance.PCEnhancer -p persistence.xml Machine.java Crane.java Bulldozer.java
Operator.java
Similarly, you should pass in the same argument to org.apache.openjpa.jdbc.meta.MappingTool:
java org.apache.openjpa.jdbc.meta.MappingTool -p persistence.xml -a buildSchema Machine.java
Crane.java Bulldozer.java Operator.java
or
java org.apache.openjpa.jdbc.meta.MappingTool -p jdo.properties -a buildSchema Machine.java Crane.java
Bulldozer.java Operator.java
You can now run the sample application. The first argument is which operation
you want the program to run. The second argument tells the application which
bootstrap system to use:
java samples.mixed.MachineMain <create | delete> <jdo |
jpa>
JPA
Sample Human Resources Model
The files for this sample are located in the
samples/persistence/models/humres directory of the OpenJPA
installation. This sample demonstrates the mapping of an example "Human
Resources" schema. The following concepts are illustrated in this sample:
Value Mappings
One to One Mappings
One to Many Mappings (with and without inverses)