When using JDO without annotations, database sequences are normally defined in the ORM file. Following discussion on the forum I was told that ObjectDB supports sequences if specified in the JDO file. Unfortunately, it doesn't seem to be supported consistently. It appears to work only in the situation that a new odb file has just been created AND something has been persisted to it. If nothing has yet been persisted, or if the odb file is not newly created, getSequence() returns null.
Code to reproduce:
import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; import javax.jdo.datastore.Sequence; import testcase.Thing; public class Testcase { public static void main(String[] args) { PersistenceManagerFactory factory = new com.objectdb.jdo.PMF(); factory.setConnectionURL("build/testcase.odb"); PersistenceManager pm = factory.getPersistenceManager(); Sequence seq1 = pm.getSequence("seq"); // per JDO spec, it should be testcase.seq if (seq1 == null) System.out.println("getSequence 1 returned null"); // it should never be null pm.currentTransaction().begin(); Thing thing = new Thing("hello"); pm.makePersistent(thing); pm.currentTransaction().commit(); Sequence seq2 = pm.getSequence("seq"); long next = seq2.nextValue(); // this works with a clean database but not an existing database System.out.println("sequence nextValue = " + next); pm.close(); factory.close(); } }
Run it twice, and it will run to completion the first time but bomb out with NullPointerException the second time.
Entity class, in package testcase:
package testcase; public class Thing { private String message; public Thing(String mess) { message = mess; } }
package.jdo (in the root package; you can verify that it is being read at run-time by introducing an error such as an invalid sequence strategy):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN" "http://java.sun.com/dtd/jdo_2_0.dtd"> <jdo> <package name="testcase"> <class name="Thing" identity-type="datastore"/> <sequence name="seq" factory-class="java.lang.Long" strategy="contiguous" /> </package> </jdo>
An additional minor issue is that ObjectDB appears to ignore the package name when the sequence is defined inside a package, but we can live with that, since changing that doesn't require changing our large existing JDO 1.2 codebase.