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’.
Beside compatibility with SQL Server 2000 and 2005, is there any other issue to use datetime2 case statement in SQL, how to return multiple variables? Error - Exclusive access could not be obtained because the database is in use How to add multiple columns to a table and add default constraint on one of them? SQL Get Latest Unique Rows What are the differences between CHECKSUM() and BINARY_CHECKSUM() and when/what are the appropriate usage scenarios?
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.
2 Solutions collect form web for “OpenJPA 1 – sequence table not being created”
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))
In my case changing the GenerationType.AUTO into GenerationType.IDENTITY solved the problem. Ofcourse this is only a solution if you can change the entity.