9.8 Auto-numbering of the primary key values
The primary key numbering functionality automatically generates the primary key value when the entity object is used to insert a record. Due to this functionality, even if the user does not specify the primary key value, a unique value is stored. Cosminexus JPA Provider provides the primary key numbering functionality.
- How to generate the primary key value
-
There are four methods for generating the primary key values:
-
TABLE
In this method, you generate the primary key values by using a table to store the primary key values.
-
SEQUENCE
In this method, you use the database sequence objects to generate the primary key values. However, if HiRDB is used as the database, implement the same processing as that of TABLE with Cosminexus JPA Provider.
-
IDENTITY
In this method, you use the identity column of the database to generate the primary key values. However, with Cosminexus JPA Provider, the operations differ depending on the database type used.
In HiRDB, you implement the same processing as that of TABLE.
In Oracle, you implement the same processing as that of SEQUENCE.
-
AUTO
You choose the generation method suitable for the database used. With Cosminexus JPA Provider, choose TABLE for both HiRDB and Oracle.
-
- Timing when the primary key values are numbered
-
In Cosminexus JPA Provider, the primary key values are numbered during the flush operation or when a transaction is committed.
- Example of SEQUENCE as the primary key value generation method
-
The following is an example of using SEQUENCE as the generation method of the primary key values. In this example, a sequence object named EMP_SEQ is assumed to have been created beforehand.
@Entity public class Employee { ... @SequenceGenerator( name="EMPLOYEE_GENERATOR", sequenceName="EMP_SEQ" ) @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEE_GENERATOR") @Column(name="EMPLOYEE_ID") public Integer getId() { return id; } ... }