Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m (Fixed annotation @GeneratedValue. Was @GeneratorValue)
 
(17 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
|toc=n
 
|toc=n
 
|eclipselink=y
 
|eclipselink=y
|eclipselinktype=JPA}}
+
|eclipselinktype=JPA
=@SequenceGenerator Annotation=
+
|api=y
If you use the [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/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:
+
|apis=
* change the allocation size to match your application requirements or database performance parameters;
+
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/GeneratedValue.html @GeneratedValue]
 +
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/SequenceGenerator.html @SequenceGenerator]
 +
}}
 +
=@SequenceGenerator=
 +
If you use the [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|<code>@GeneratedValue</code>]] 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; (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
Line 32: Line 37:
 
  <td>'''<tt>sequenceName</tt>'''</td>
 
  <td>'''<tt>sequenceName</tt>'''</td>
 
  <td>A <tt>String</tt> name of the sequence</td>
 
  <td>A <tt>String</tt> name of the sequence</td>
  <td><tt>SequenceGenerator</tt></td>
+
  <td><tt>SequenceGenerator.name</tt></td>
 
  <td>No</td>
 
  <td>No</td>
 
</tr>
 
</tr>
Line 38: Line 43:
  
  
The following example shows how to use this annotation to specify the allocation size for the <tt>SEQUENCE</tt> primary key generator named <tt>Cust_Seq</tt>.
+
The following example shows how to use this annotation to specify the allocation size for the <tt>SEQUENCE</tt> primary key generator named <tt>Emp_Seq</tt>.
  
 
======'' Example: @SequenceGenerator''======
 
======'' Example: @SequenceGenerator''======
Line 46: Line 51:
 
     ...
 
     ...
 
     @Id
 
     @Id
     @SequenceGenerator(name="Cust_Seq", allocationSize=25)
+
     @SequenceGenerator(name="Emp_Seq", allocationSize=25)
     @GeneratorValue(strategy=SEQUENCE, generator="Cust_Seq")
+
     @GeneratedValue(strategy=SEQUENCE, generator="Emp_Seq")
     @Column(name="CUST_ID")
+
     @Column(name="EMP_ID")
 
     public Long getId() {
 
     public Long getId() {
 
         return id;
 
         return id;
Line 56: Line 61:
 
</source>
 
</source>
  
 +
======''Example: Using <code><nowiki><sequence-generator></nowiki></code>''======
  
For more information on the EclipseLink artifacts configured by these JPA metadata, refer to [[Introduction%20to%20Descriptors%20(ELUG)#Descriptors and Sequencing|Descriptors and Sequencing]].
+
<source lang="xml">
 +
<entity class="Employee">
 +
    <attributes>
 +
        <id name="id">
 +
            <column name="EMP_ID"/>
 +
            <generated-value generator="Emp_Seq" strategy="SEQUENCE"/>
 +
            <sequence-generator name="Emp_Seq" allocationSize="25"/>
 +
        </id>
 +
        ...
 +
    </attributes>
 +
</entity>
 +
</source>
  
  
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/TableGenerator|@TableGenerator]]
+
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Ids/TableGenerator|@TableGenerator]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic_Mappings|Basic Mappings]]
+
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/UuidGenerator|@UuidGenerator]]
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity|Entity]]
+
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Latest revision as of 11:41, 21 November 2017

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug 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.name No


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

Example: @SequenceGenerator
 @Entity
 public class Employee implements Serializable {
     ...
     @Id
     @SequenceGenerator(name="Emp_Seq", allocationSize=25)
     @GeneratedValue(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">
            <column name="EMP_ID"/>
            <generated-value generator="Emp_Seq" strategy="SEQUENCE"/>
            <sequence-generator name="Emp_Seq" allocationSize="25"/>
        </id>
        ...
    </attributes>
</entity>


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

Back to the top