You use this procedure to generate primary keys for entities automatically. The JPA specification defines four strategies for primary key generation. These strategies are specified using the @GeneratedValue annotation, for example, @GeneratedValue(strategy=GenerationType.TABLE, generator="myGenerator").
The generation strategies have the following specifics:
● Auto – the container picks a strategy and takes care of primary key generation.
The JPA implementation underlying SAP NetWeaver Application Server defaults to the ID generation strategy TABLE. All default rules for the ID generation strategy TABLE apply, especially these for the table name and the names of the primary key and the value columns.
● 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.
The TABLE ID generation strategy relies on the existence of a database table that manages ID values. The table must have two columns. The first, that is, primary key column, identifies the entity class for which ID values are generated. It should have the JDBC type VARCHAR. The second, that is, 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).
...
1. (Optional) Specify the name of the generator table, using the table property of the @TableGenerator annotation.
@Entity
public class Employee {
@Id
@GeneratedValue (strategy=TABLE, generator="myGen")
@TableGenerator(name="myGen", table="ID_GEN")
int id;
}
If the table name is not explicitly specified, it defaults to the name TMP_SEQUENCE.
Alternatively, you can specify the name of the generator table using the property com.sap.engine.services.orpersistence.generator.auto.tablename in the persistence.xml. The table name is specified in the value attribute of the property as shown in the example below:
<?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.engine.services.orpersistence.generator.auto.tablename" value="MY_GENERATOR_TABLE"/>
</properties>
</persistence-unit>
</persistence>
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.engine.services.orpersistence.generator.auto.tablename property in the persistence.xml.
2. (Optional) Specify the name of the primary key column, the name of the value column and the primary key value of the generator table, using the properties pkColumnName, valueColumnName and pkColumnValue of the @TableGenerator annotation.
If these properties are not specified, the following default values apply:
Property |
Default Value |
pkColumnName |
GEN_KEY |
valueColumnName |
GEN_VALUE |
pkColumnValue |
The fully-qualified class name of the entity |
...
1. To use the SEQUENCE ID generation strategy, specify the generation type, using the strategy property of the @GeneratedValue annotation.
2. Specify the name of the database sequence object to generate IDs from, using the sequenceNameproperty of the @SequenceGenerator annotation.
@Entity
@SequenceGenerator(name="SequenceGenerator", sequenceName="mySequence")
public class Employee {
private Integer id;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SequenceGenerator")
public Integer getId() {
return id;
}
}
You can use the SEQUENCE ID generation strategy only with the following databases:
■ Oracle
■ MaxDB
■ DB2 for z/OS
■ DB2 for i5/OS
■ DB2 for Linux, Unix, Windows
The SEQUENCE ID generation strategy is not possible if you use Open SQL to connect to the database.
To use the Identity ID generation strategy, specify the generation type, using the strategy property of the @GeneratedValue annotation. You do not need to declare a generator explicitly.
@Entity
public class Employee {
private Integer id;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
}
You can use the IDENTITY ID generation strategy only with the following databases:
■ SQL Server
■ DB2 for z/OS
■ DB2 for i5/OS
■ DB2 for Linux, Unix, Windows