OpenJPA, HSQLDB and proper shutdown

I’m starting to get tired of OpenJPA/HSQLDB setup that I’ve trying: it seems to give more problems than it solves. Now I discovered that the HSQL db wasn’t been properly shutdown and for reasons that I’m investigating, if the HSQL db is not propertly closed (SHUTDOWN command) it’s impossible to reopen it again. It fails with a IndexOutOfBoundsException. Caused by: org.hsqldb.HsqlException: error in script file line: 33 java.io.IOException: java.lang.IndexOutOfBoundsException in statement [SET TABLE PUBLIC.NOTES INDEX '2883070'] at org.hsqldb.Error.error(Error.java:111) at org.hsqldb.scriptio.ScriptReaderText.readDDL(ScriptReaderText.java:132) at org.hsqldb.scriptio.ScriptReaderBase.readAll(ScriptReaderBase.java:88) at org.hsqldb.persist.Log.processScript(Log.java:721) at org.hsqldb.persist.Log.open(Log.java:187) at org.hsqldb.persist.Logger.openPersistence(Logger.java:209) at org.hsqldb.Database.reopen(Database.java:265) at org.hsqldb.Database.open(Database.java:235) at org.hsqldb.DatabaseManager.getDatabase(DatabaseManager.java:222) at org.hsqldb.DatabaseManager.newSession(DatabaseManager.java:145) at org.hsqldb.jdbc.JDBCConnection.<init>(JDBCConnection.java:3219) ... 59 more Annoying. This is not OpenJPA fault, but it gets complicated to actually solve it. ...

January 14, 2010

OpenJPA: Generated SQL contains extra UPDATEs

I’m trying to use OpenJPA to insert some entries in the database and I’m getting a strange number of UPDATEs beside the INSERTs. I isolated the problem to the following snippet of code private void start() { EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistencexmltest1PU"); EntityManager em = emf.createEntityManager(); for (int i = 0; i < 10; i**) { em.getTransaction().begin(); MyEntity n =new MyEntity(); n.setValue(i); em.persist(n); em.getTransaction().commit(); } } The generated SQL looks like this: ...

September 18, 2009

OpenJPA Enhancer Ant task in a Netbeans project

In the build.xml of the project (likely this will be a Java Class Library project), override -post-compile or -pre-jar and invoke <openjpac/>. You will need to add the build/classes and the openjpa jars to <taskdef/> and <openjpac/>. The <openjpac/> will enhance all classes mentioned in persistence.xml (which has to be in the classpath) <?xml version="1.0" encoding="UTF-8"?> <project name="JpaEntitiesLibrary" default="default" basedir="."> <description>Builds, tests, and runs the project JpaEntitiesLibrary.</description> <import file="nbproject/build-impl.xml"/> <target name="-post-compile"> <echo message="begin openJPAC"/> <path id="openjpa.path.id"> <pathelement location="${build.classes.dir}"/> <!-- Adding the OpenJPA jars into the classpath --> <fileset dir="C:\Users\ecerulm\Downloads\apache-openjpa-1.2.1-binary\apache-openjpa-1.2.1\lib" includes="*.jar"/> <!-- or if you create a OpenJPA Library you can use that instead --> <!--<pathelement path="${libs.OpenJPA.classpath}"/>--> </path> <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask"> <classpath refid="openjpa.path.id"/> </taskdef> <openjpac> <classpath refid="openjpa.path.id"/> </openjpac> <echo message="end openJPAC"/> </target> </project> You can refer to the OpenJPA jars by either a <fileset> with the file path to the OpenJPA directory or by refererring to a Netbeans Library instead. You can create a OpenJPA Library via Tools ⇒ Libraries ⇒ New Library and add all the jars to the Library. If you name the library “OpenJPA” then you can refer to its classpath with <pathelement path="${libs.OpenJPA.classpath}"/>. See the commented code in the build.xml above. ...

September 16, 2009