Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
@GeneratedValue
Use the @GeneratedValue annotation to enable the EclipseLink persistence provider to generate unique identifiers for entity primary keys (see @Id).
For more information, see Section 11.1.17 "GeneratedValue Annotation" in the JPA Specification.
Use the @GeneratedValue annotation to:
- Override the type of identity value generation selected by the persistence provider for your database if you feel another generator type is more appropriate for your database or application
- Override the primary key generator name selected by the persistence provider if this name is awkward, a reserved word, incompatible with a preexisting data model, or invalid as a primary key generator name in your database
The @GeneratedValue annotation has the following attributes:
Attribute | Description | Default | Required? |
---|---|---|---|
generator | The name of the SequenceGenerator or TableGenerator object use for this generated value.
If no generator is defined for this name, a generator is defaulted. For a SEQUENCE generator the name becomes the name of the database sequence object. For a TABLE generator the name becomes the value for the sequence row in the sequence table. |
SEQ_GEN | No |
strategy | By default, EclipseLink persistence provider uses a TABLE generator. To use another generator type, set the value of this attribute to one of the following enumerated values of the GenerationType enumerated type:
|
TABLE | No |
Note: By default, EclipseLink chooses the TABLE strategy using a table named SEQ_GEN_TABLE, with SEQ_NAME and SEQ_COUNT columns, with allocationSize of 50 and pkColumnValue of SEQ_GEN. The default SEQUENCE used is database sequence SEQ_GEN_SEQUENCE with allocationSize of 50. Note that the database sequence increment must match the allocation size.
Note: IDENTITY strategy is database specific and not supported on all database. IDENTITY is supported on Sybase, DB2, SQL Server, MySQL, Derby, JavaDB, Informix, and Postgres databases.
Note: There is a difference between using IDENTITY and other id generation strategies: the identifier will not be accessible until after the insert has occurred – it is the action of inserting that caused the identifier generation. Due to the fact that insertion of entities is most often deferred until the commit time, the identifier would not be available until after the transaction has been committed.
Note: We do not recommend using the IDENTITY strategy for it does not support preallocation. This affects performance and usability.
Note: Be careful when using the automatic ID generation: the persistence provider has to pick its own strategy to store the identifiers, but it needs to have a persistent resource, such as a table or a sequence, to do so. The persistence provider cannot always rely upon the database connection that it obtains from the server to have permissions to create a table in the database. This is usually a privileged operation that is often restricted to the DBA. There will need to be a creation phase or schema generation to cause the resource to be created before the AUTO strategy can function.
The following example shows how to use table id generation. This will cause EclipseLink persistence provider to create an identifier value from a sequence table and insert it into the id field of each Employee entity that gets persisted.
Example: Using @GeneratedValue
@Entity public class Employee implements Serializable { @Id @GeneratedValue(strategy=GenerationType.TABLE) private long id; ... }