Monday, November 2, 2009

@TableGenerator - ID for your entities

@TableGenerator annotation lets you define auto generated id columns for your entities.
In a recent project with MySQL as the backend , I couldn’t create a sequence and tie it up with the JPA entity ( Still wondering how to do that…)
To resolve this, I made use of @TableGenerator annotation , created a table to store the latest id and tied it up with the entity.

private long studentId;

The @TableGenerator annotation defines that “TBL_KEYS” is the table to store the generated id for the entity.
STD_KEY is the column to store the generated id
STD_KEY_NAME is the column which has the key-name entries. The key name here is “studentkey” .
You can add another entry in TBL_KEYS named “mark_key” and map it to another entity TBL_MARKS.
@GeneratedValue annotation specifies that the id generated is of GenerationType.TABLE type and maps the already defined @TableGenerator with given name.

No comments:

Post a Comment