When to use @ID & @GeneratedValue annotations in Appian?
When to use @ID & @GeneratedValue annotations in Appian?
Answer
We use the @Id and @GeneratedValue annotations when you map a Custom Data Type (CDT) to a database table. They define the primary key and how its value is assigned when a record is created.
@Id :
@Id marks the CDT field that is the primary key, the value that uniquely identifies each row in the table. Use it on its own when your application or another system supplies the key, so the database does not generate it.
@GeneratedValue:
@GeneratedValue goes alongside @Id when the key should be generated automatically, typically by a database auto-increment column. It works only if the database and your Appian configuration support that strategy.
Example CDT configuration
Xml Code:
Here @Id makes formId the primary key, and @GeneratedValue tells Appian the value is generated automatically. @Column maps the field to the FORM_ID column and sets its properties. When a new form record is saved, the database might assign FORM_ID = 101. The actual number depends on the current sequence or auto-increment state.
Scenario | Configuration |
|---|---|
The application supplies the primary key |
|
The database generates the primary key |
|
The CDT maps to an existing table | Check the table’s existing primary key and generation strategy first |
Common mistake
Adding @GeneratedValue does not make the database generate identifiers. The column and the CDT mapping have to agree. With MySQL, for example, the column itself must be set up as auto-increment.
Official Appian documentation: Mapping CDTs to Pre-defined Database Tables