Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator"

(@SequenceGenerator)
Line 11: Line 11:
 
=@SequenceGenerator=
 
=@SequenceGenerator=
 
If you use the [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|@GeneratedValue]] annotation to specify a primary key generator of type <tt>SEQUENCE</tt>, then you can use the <tt>@SequenceGenerator</tt> annotation to fine-tune this primary key generator to do the following:
 
If you use the [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|@GeneratedValue]] annotation to specify a primary key generator of type <tt>SEQUENCE</tt>, then you can use the <tt>@SequenceGenerator</tt> annotation to fine-tune this primary key generator to do the following:
* change the allocation size to match your application requirements or database performance parameters;
+
* change the allocation size to match your application requirements or database performance parameters; This should match your database sequence <code>INCREMENT</code>
 
* change the initial value to match an existing data model (for example, if you are building on an existing data set for which a range of
 
* change the initial value to match an existing data model (for example, if you are building on an existing data set for which a range of
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable

Revision as of 10:10, 14 June 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

@SequenceGenerator

If you use the @GeneratedValue annotation to specify a primary key generator of type SEQUENCE, then you can use the @SequenceGenerator annotation to fine-tune this primary key generator to do the following:

  • change the allocation size to match your application requirements or database performance parameters; This should match your database sequence INCREMENT
  • change the initial value to match an existing data model (for example, if you are building on an existing data set for which a range of
@SequenceGenerator Attributes
Attribute Description Default Required?
name The name of the generator must match the name of a GeneratedValue with its strategy attribute set to SEQUENCE. Yes
allocationSize An int value that must match the increment size of the database sequence object. 50 No
initialValue An int value to start all primary keys. 0 No
sequenceName A String name of the sequence SequenceGenerator No


The following example shows how to use this annotation to specify the allocation size for the SEQUENCE primary key generator named Cust_Seq.

Example: @SequenceGenerator
 @Entity
 public class Employee implements Serializable {
     ...
     @Id
     @SequenceGenerator(name="Emp_Seq", allocationSize=25)
     @GeneratorValue(strategy=SEQUENCE, generator="Emp_Seq")
     @Column(name="EMP_ID")
     public Long getId() {
         return id;
     }
     ...
 }
Example: Using <sequence-generator>
<entity class="Employee">
    <attributes>
        <id name="id">
            <generated-value generator="Emp_Seq" strategy="SEQUENCE"/>
            <sequence-generator name="Emp_Seq" allocationSize="25"/>
            <column name="EMP_ID"/>
        </id>
        ...
    </attributes>
</entity>


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...