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.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue"
m |
m |
||
Line 4: | Line 4: | ||
|eclipselink=y | |eclipselink=y | ||
|eclipselinktype=JPA}} | |eclipselinktype=JPA}} | ||
− | = | + | =@GeneratedValue= |
Use the <tt>@GeneratedValue</tt> annotation to enable the EclipseLink persistence provider to generate unique identifiers for entity primary keys (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]]). | Use the <tt>@GeneratedValue</tt> annotation to enable the EclipseLink persistence provider to generate unique identifiers for entity primary keys (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]]). |
Revision as of 14:21, 3 May 2011
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 9.1.9 "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 default value of this attribute is the name that EclipseLink persistence provider assigns to the primary key generator it selects. If the generator name is awkward, a reserved word, incompatible with a preexisting data model, or invalid as a primary key generator name in your database, set the value of this attribute to the String generator name you want to use. |
The name that EclipseLink persistence provider assigns to the primary key generator it selects | No |
strategy | By default, EclipseLink persistence provider chooses the type of primary key generator that is most appropriate for the underlying database. If you feel that another generator type is more appropriate for your database or application, set the value of this attribute to one of the following enumerated values of the GenerationType enumerated type:
|
The type of primary key generator that is most appropriate for the underlying database | No |
The following example shows how to use automatic id generation. This will cause EclipseLink persistence provider to create an identifier value 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.AUTO) private int id; ... }
Warning: 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.