On Java Development

All things related to Java development, from the perspective of a caveman.

Archive for November, 2015

Persisting Records with Auto Generated Fields

without comments


This post shows what can happen when adding records to a file with an identity field and the value of for the field is automatically incremented by the database.

There are certain annotations that need to be made to tell Hibernate not to update that field.


The SQL Statement Segment

Below is the source segment used to create the table. The Library/FileName is an arbitrary name of the library and file.

The GENERATED ALWAYS AS IDENTITY clause indicates that DB2 will always generate a unique value for the identity column for each row inserted into the table. DB2/400 will issue an error if a value is specified in an insert operation for the identity column defined using this clause. In other words, your Java code should never modify this field. If it does, you should probably expect unexpected consequences for the file.


The Entity Class

Below is the source segment of the entity class showing the column definition for the identity field

Adding ‘insertable = false, updatable = false’ to the column definition indicates that the responsibility for calculating the sequence number does not belong to the application. DB2/400, or DB2 Universal Database as it is now known, is responsible for maintaining the value for this field. In effect, the field obtains a read only characteristic.


Errors Generated During Record Insert

Without ‘insertable = false, updatable = false’, when the record is persisted to the database, the following error is generated.



Generating Unique Values in DB2 Universal Database
Apache OpenJPA 2.1 User’s Guide, Chapter 13, Mapping Metadata, Column
IBM Knowledge Center – Apache OpenJPA User’s Guide

Written by admin

November 13th, 2015 at 7:23 am