Search code examples
javasql-serverjpasql-server-2008-r2openjpa

OpenJPA 1 - sequence table not being created


I have an entity class with the following annotation on its primary key: @GeneratedValue(strategy = GenerationType.AUTO). However, when I try to persist an instance of this class, I get

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'OPENJPA_SEQUENCE_TABLE'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)

The table it's looking for definitely does not exist in the database. The user it's connecting to the database as can create tables. Should it be creating OPENJPA_SEQUENCE_TABLE automatically, or do I have to do that for it? If so, what's the table schema it's expecting? I'm using openjpa-1.2.2.jar.

Edit: I looked at main()'s JavaDoc since it has an option to add the sequence table on the command line, but org.apache.openjpa.jdbc.schema.TableJDBCSequence does not exist in openjpa-1.2.2.jar. org.apache.openjpa.jdbc.schema does, but TableJDBCSequence is not in it.


Solution

  • By default, OpenJPA doesn't create any of the tables (including the sequence table) automatically unless you have the SynchronizeMappings property enabled in your persistence.xml. However, this is really only good for debugging purposes since I think it resets the database each time the application starts up:

    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    

    You can also use the OpenJPA MappingTool from the command line to generate the schema script:

    java org.apache.openjpa.jdbc.meta.MappingTool -action buildSchema -foreignKeys true
    

    More information about the Mapping Tool and runtime forward mapping is available here: http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_mapping.html#ref_guide_ddl_examples

    Your final option is to just create the table manually. Here is the script for Oracle; you can probably convert it to SQL Server fairly easily:

    CREATE TABLE openjpa_sequence_table (ID tinyint(4) NOT NULL, SEQUENCE_VALUE bigint(20) default NULL, PRIMARY KEY (ID))