We saw in the JPA Tutorial the overall process for adding persistence to a simple application. In that tutorial we persisted the objects to an RDBMS. Here we show the differences when persisting instead to the db4o object datastore.
The tutorial guides you through this. You can obtain the code referenced in this tutorial from SourceForge (one of the files entitled "datanucleus-samples-tutorial-*").
The model classes are exactly the same here, so please refer to the JPA Tutorial for details.
When defining the persistence of the classes, the only difference would be that we omit the orm.xml file since we aren't doing object-relational mapping with db4o.
In the JPA Tutorial we saw that we need to bytecode enhance our classes to be persisted. This step is identical when using db4o so please refer to the original tutorial for details.
With db4o we have no such concept as a "schema" and so this step is omitted.
We saw in the JPA Tutorial how to persist our objects using JPA API calls. This is the same when using db4o so please refer to the original tutorial for details.Please note that DataNucleus doesn't currently support use of JPQL/SQL with db4o so any operations for querying will throw an Exception informing you of this.
We saw in the JPA Tutorial how to run our JPA-enabled application. This is very similar when persisting to db4o. The only differences are
The persistence.xml file could be changed to be like this
<?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>com/datanucleus/samples/jpa/tutorial/orm.xml</mapping-file> <class>org.datanucleus.samples.jpa.tutorial.Product</class> <class>org.datanucleus.samples.jpa.tutorial.Book</class> <properties> <property name="datanucleus.ConnectionURL" value="db4o:file:db4o.db"/> </properties> </persistence-unit> </persistence>
As you can see changing between an RDBMS and db4o is trivial. You don't need to change your actual classes, or persistence code at all. It's simply a change to your runtime details.
If you have any questions about this tutorial and how to develop applications for use with DataNucleus please read the online documentation since answers are to be found there. If you don't find what you're looking for go to our Forums.
The DataNucleus Team