Show TOC

Background documentationGenerated Primary Keys Locate this document in the navigation structure

 

Every entity must have a unique identifier (ID). The ID values can be either set by the application or they can be automatically generated by the JPA runtime, using an “ID generator”. The JPA specification defines four strategies for ID generation. These strategies are specified using the strategy element of the @GeneratedValue annotation, for example @GeneratedValue(strategy=GenerationType.TABLE, generator="myGenerator").

The generation strategies have the following specifics:

  • TABLE — uses a dedicated database table to generate and store primary keys.

  • SEQUENCE — uses a database sequence to generate primary keys.

  • IDENTITY — uses a database identity column to generate primary keys.

  • AUTO — the JPA container picks a suitable strategy and takes care of the primary key generation. The SAP implementation of JPA uses the TABLE strategy.

TABLE Generation Strategy

The TABLE ID generation strategy relies on the existence of a database table that manages ID values. This table must have two columns. The first column, the “primary key column”, identifies the entity class for which ID values are generated. It must have the JDBC type VARCHAR. The second column, the “value column”, stores the maximum primary key values managed by the ID generator. It must have one of the following JDBC types: SMALLINT, INTEGER, BIGINT, or DECIMAL (length, 0).

Syntax Syntax

  1. @Entity
    public class Employee {
    	@Id
    	@GeneratedValue(strategy=TABLE, generator="myGen")
    	@TableGenerator(name="myGen", table="ID_GEN")
    	int id;
    	// ...
    }
End of the code.

If the table name is not explicitly specified, it defaults to the name TMP_SEQUENCE.

Note Note

Alternatively, you can specify the name of the generator table using the property com.sap.jpa.identity.generator.tablename in the persistence-unit section of the persistence.xml file. The table name is specified in the value attribute of the property, as shown in the example below:

End of the note.

Syntax Syntax

  1. <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0">
    	<persistence-unit name="MyPersistenceUnit">
    		<jta-data-source>MY_DATA_SOURCE</jta-data-source>
    		<properties>
    			<property name="com.sap.jpa.identity.generator.tablename" value="MY_GENERATOR_TABLE">
    		</properties>
    	<persistence unit>
    </persistence>
End of the code.

Recommendation Recommendation

To keep the data of individual applications separate, we recommend that you specify the table name either using the property table of the @TableGenerator annotation or using the com.sap.jpa.identity.generator.tablename property in the persistence.xml.

End of the recommendation.

Optionally, you can specify the name of the primary key column, the name of the value column and the value of the primary of the generator table, using the attributes pkColumnName, valueColumnName and pkColumnValue of the @TableGenerator annotation. If these elements are not specified explicitly, the following default rules apply:

Attribute

Default Value

table

TMP_SEQUENCE

pkColumnName

GEN_KEY

valueColumnName

GEN_VALUE

pkColumnValue

The fully qualified class name of the entity.

SEQUENCE Generation Strategy

The SEQUENCE ID generation strategy is specified using the @SequenceGenerator annotation. The sequenceName attribute determines the database sequence object that is used for the generation of IDs.

You have to create the sequence object on the database manually. The DDL generation feature of the SAP implementation of JPA does not create the required DDL either.

More information: Creating Entities and Generating Database Tables

Syntax Syntax

  1. @Entity
    @SequenceGenerator(name="SequenceGenerator", sequenceName="mySequence")
    public class Employee {
    	private int id;
    	// ...
    	@Id
    	@GeneratedValue(strategy=GeneratorType.SEQUENCE, generator="SequenceGenerator")
    	public int getId() {
    		return id;
    	}
    	// ...
    }
End of the code.

Caution Caution

You must set the attributes of the @SequenceGenerator annotation (especially allocationSize and initialValue, which are optional) to be consistent with the sequence definition on the database.

This is due to the JPA-default values of allocationSize (50) and initialValue (1).

End of the caution.

Note Note

The SEQUENCE ID generation strategy is not possible if you use Open SQL to connect to the database.

You can use the SEQUENCE ID generation strategy only with the following databases:

  • SAP MaxDB

  • IBM DB2 for Linux, UNIX and Windows

  • IBM DB2 for z/OS

  • IBM DB2 for i

  • Oracle

End of the note.
IDENTITY Generation Strategy

To use the IDENTITY ID generation strategy, you do not need to declare an ID generator explicitly. You only specify the strategy attribute of the @GeneratedValue annotation.

Syntax Syntax

  1. @Entity
    public class Employee {
    	private int id;
    	// ...
    	@Id
    	@GeneratedValue(strategy=GenerationType.IDENTITY)
    	public int getId() {
    		return id;
    	}
    	// ...
    }
End of the code.

Note Note

The IDENTITY ID generation strategy is not possible if you use Open SQL to connect to the database.

You can use the IDENTITY ID generation strategy only with the following databases:

  • MS SQL Server

  • IBM DB2 for Linux, UNIX and Windows

  • IBM DB2 for z/OS

  • IBM DB2 for i

End of the note.
AUTO Generation Strategy

For AUTO, the SAP implementation of JPA defaults to the ID generation strategy TABLE. All default rules for the TABLE generation strategy, apply especially those for the table name and the names of the primary key and the value columns.