Creating primary keys for a JPA entity
Before you use entity manager to make a JPA entity persistent, we must create a primary key for the entity.
Task info
If you have a JPA DAO implementation which extends AbstractJPAEntityDaoImpl, you can use myJPADao.generatePrimaryKey("MyTable") to generate primary key for our new JPA entity. If your JPA DAO implementation does not extend AbstractJPAEntityDaoImpl, we can use ECKeyManager.singleton().getNextKey("MyTable") to generate a primary key for our 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
- BIGINT
- (Oracle) NUMBER(38, 0))
use the getNextKeyAsLong method.
Related concepts
Primary keys
Finding data using an access bean