OpenJPA 1 - sequence table not being created

有些话、适合烂在心里 提交于 2019-12-11 02:24:05

问题


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.


回答1:


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)) 



回答2:


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.



来源:https://stackoverflow.com/questions/9874500/openjpa-1-sequence-table-not-being-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!