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 (→@GeneratedValue Annotation) |
m (→@GeneratedValue Annotation) |
||
Line 15: | Line 15: | ||
|content= | |content= | ||
<tr> | <tr> | ||
− | <td>'''generator'''</td> | + | <td>'''<tt>generator</tt>'''</td> |
− | <td>The default value of this attribute is the name that EclipseLink persistence provider assigns to the primary key generator it selects.<br>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 <tt>String</tt> generator name you want to use | + | <td>The default value of this attribute is the name that EclipseLink persistence provider assigns to the primary key generator it selects.<br>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 <tt>String</tt> generator name you want to use.</td> |
<td>The name that EclipseLink persistence provider assigns to the primary key generator it selects</td> | <td>The name that EclipseLink persistence provider assigns to the primary key generator it selects</td> | ||
<td>No</td> | <td>No</td> | ||
</tr> | </tr> | ||
− | + | <tr> | |
− | + | <td>'''<tt>strategy</tt>'''</td> | |
− | + | <td>By default, EclipseLink persistence provider chooses the type of primary key generator that is most appropriate for the underlying database.<br>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 <tt>GenerationType</tt> enumerated type: | |
− | + | ||
<ul> | <ul> | ||
− | |||
− | |||
− | |||
<li><tt>AUTO</tt> (default) – specify that EclipseLink persistence provider should choose a primary key generator that is most appropriate for the underlying database.<br /><br />{{EclipseLink_Note|note=By default, EclipseLink chooses the <tt>TABLE</tt> strategy using a table named <tt>SEQ_GEN_TABLE</tt>, with <tt>SEQ_NAME</tt> and <tt>SEQ_COUNT</tt> columns, with <tt>allocationSize</tt> of 50 and <tt>pkColumnValue</tt> of <tt>SEQ_GEN</tt>. The default <tt>SEQUENCE</tt> used is database sequence <tt>SEQ_GEN_SEQUENCE</tt> with <tt>allocationSize</tt> of 50. Note that the database sequence increment must match the allocation size.}}</li> | <li><tt>AUTO</tt> (default) – specify that EclipseLink persistence provider should choose a primary key generator that is most appropriate for the underlying database.<br /><br />{{EclipseLink_Note|note=By default, EclipseLink chooses the <tt>TABLE</tt> strategy using a table named <tt>SEQ_GEN_TABLE</tt>, with <tt>SEQ_NAME</tt> and <tt>SEQ_COUNT</tt> columns, with <tt>allocationSize</tt> of 50 and <tt>pkColumnValue</tt> of <tt>SEQ_GEN</tt>. The default <tt>SEQUENCE</tt> used is database sequence <tt>SEQ_GEN_SEQUENCE</tt> with <tt>allocationSize</tt> of 50. Note that the database sequence increment must match the allocation size.}}</li> | ||
<li><tt>TABLE</tt> – specify that EclipseLink persistence provider assign primary keys for the entity using an underlying database table to ensure uniqueness (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/TableGenerator|@TableGenerator]]).</li> | <li><tt>TABLE</tt> – specify that EclipseLink persistence provider assign primary keys for the entity using an underlying database table to ensure uniqueness (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/TableGenerator|@TableGenerator]]).</li> | ||
Line 35: | Line 31: | ||
{{EclipseLink_Note|note=There is a difference between using <tt>IDENTITY</tt> 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.}} | {{EclipseLink_Note|note=There is a difference between using <tt>IDENTITY</tt> 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.}} | ||
{{EclipseLink_Note|note=We do not recommend using the <tt>IDENTITY</tt> strategy for it does not support preallocation.}}</li> | {{EclipseLink_Note|note=We do not recommend using the <tt>IDENTITY</tt> strategy for it does not support preallocation.}}</li> | ||
− | |||
</ul> | </ul> | ||
+ | </td> | ||
+ | <td>The type of primary key generator that is most appropriate for the underlying database</td> | ||
+ | <td>No</td> | ||
+ | </tr> | ||
− | + | }} | |
+ | This example shows how to use automatic id generation. This will cause EclipseLink persistence provider to create an identifier value and insert it into the <tt>id</tt> field of each <tt>Employee</tt> entity that gets persisted.<br> | ||
<span id="Example 18-7"></span> | <span id="Example 18-7"></span> | ||
''''' Using Automatic Id Generation''''' | ''''' Using Automatic Id Generation''''' | ||
Line 53: | Line 53: | ||
</source> | </source> | ||
<br> | <br> | ||
− | {{EclipseLink_Warning|warning=Be careful when using the automatic | + | {{EclipseLink_Warning|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 <tt>AUTO</tt> strategy can function.}} |
Revision as of 13:47, 17 June 2010
@GeneratedValue Annotation
Use the @GeneratedValue annotation to enable 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.
This annotation lets you do the following:
- 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 |
This 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.
Using Automatic Id Generation
@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.