I have database utility classes in one project, the id's use sequences declared in an orm.xml file for example:
<entity class="com.x.y.z.model.Unit"> <attributes> <id name="unitId"> <column name="unit_id" /> <generated-value strategy="SEQUENCE" generator="unit_seq"/> <sequence-generator name="unit_seq" sequence-name = "oe.seq_unit_id" allocation-size = "1" /> </id> </attributes> </entity>
and the persistence.xml references this orm.xml :
<persistence-unit name="OBJECT_DB_LOCAL_PU" transaction-type="JTA"> <provider>com.objectdb.jpa.Provider</provider> <mapping-file>/META-INF/orm.xml</mapping-file> <properties> <property name="javax.persistence.jdbc.url" value="$objectdb/db/gdm.odb"/> <property name="javax.persistence.jdbc.user" value="admin"/> <property name="javax.persistence.jdbc.password" value="admin"/> </properties> </persistence-unit>
both persistence.xml and orm.xml are located in src/main/resources/META-INF and Maven correctly copies META-INF and all contents into the root of jar file when the (EJB) project is built.
When that EJB is deployed to e.g. GlassFish 3.1.1 all apperar to be OK, and a singleton annotated with @Startup can access the database OK for both read and write, suggesting that the orm.xml is found OK and that the sequences are declared properly (they also work for postgres).
However, when a different web project (WAR file), which declares a dependency to the database EJB is built and deployed, this error message pops up:
Caused by: com.objectdb.o._PersistenceException: Failed to read a resource '/META-INF/orm.xml' (referenced from persistence.xml)
One partial work-around seems to be to add a copy of orm.xml into src/main/resources/META-INF of the web project that depends on the EJB .. but it's not great to have duplicate copies of the file, and this isn't needed for other persistence providers, e.g. EclipseLink, which can pick up the orm.xml from the jar file embedded in the war file OK.
However, even when duplicating the orm.xml, a different error pops up:
WARNING: DTX5014: Caught exception in beforeCompletion() callback: [ObjectDB 2.2.9_08] javax.persistence.PersistenceException Invalid null value in non optional field unitId (error 648)
which seems to imply that the sequence generator isn't working when the EJB is embedded in a WAR file?