OpenJPA + Oracle Identity Column

configuring OpenJPA + Oracle identity column

What is OpenJPA?

Apache OpenJPA is a Java persistence project at The Apache Software Foundation that can be used as a stand-alone POJO persistence layer or integrated into any Java EE compliant container and many other lightweight frameworks, such as Tomcat and Spring.
[Reference]

What is Oracle identity column?

Oracle 12c introduced a new way that allows you to define an identity column for a table, which is similar to the AUTO_INCREMENT column in MySQL or IDENTITY column in SQL Server.

The identity column is very useful for the surrogate primary key column. When you insert a new row into the identity column, Oracle auto-generates and insert a sequential value into the column.
[Reference]

Mapping primary fields of entities done with @Id annotation:

@Id 
private long id;

But OpenJPA will do this mapping with it’s default configuration. To customize identity mapping there is @GeneratedValue annotation.

To use Oracle Identity column:

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

Typically, OpenJPA auto-configures its JDBC behavior and SQL dialect for your database, based on the values of your connection-related configuration properties. There is some built-in dictionaries and for Oracle database there is:

org.apache.openjpa.jdbc.sql.OracleDictionary

There is some parameters in this dictionary, to use identity column feature it is important to set Supportsgetgeneratedkeys to true:

supportsgetgeneratedkeys=true

Java Developer