Creating primary keys for a JPA entity

Before you use entity manager to make a JPA entity persistent, you must create a primary key for the entity.

About this task

If you have a JPA DAO implementation which extends AbstractJPAEntityDaoImpl, you can use myJPADao.generatePrimaryKey("MyTable") to generate primary key for your new JPA entity. If your JPA DAO implementation does not extend AbstractJPAEntityDaoImpl, you can use ECKeyManager.singleton().getNextKey("MyTable") to generate a primary key for your new JPA entity.

Procedure

Create an entry in the KEYS table. The following is an example SQL statement to make the entry in the KEYS table.

insert into KEYS (TABLENAME, COUNTER, KEYS_ID) 
   values ("table_name", 0, 1)

Note that with the preceding SQL statement, default values for the other columns in the KEYS table are accepted. The value for COUNTER indicates the value at which the count should start. The value for KEYS_ID should be any positive value.

If your primary key is defined as a long data type
  • DB2 BIGINT
  • OracleNUMBER(38, 0))
use the getNextKeyAsLong method.