Jump to: navigation, search

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

m (New page: =@SequenceGenerator Annotation= {{EclipseLink_JPA |previous=@TableGenerator |next= [[EclipseLink/Use...)
 
m (@SequenceGenerator Annotation)
Line 1: Line 1:
 
=@SequenceGenerator Annotation=
 
=@SequenceGenerator Annotation=
 +
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:
 +
* change the allocation size to match your application requirements or database performance parameters;
 +
* 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
 +
 +
 +
The <tt>@SequenceGenerator</tt> annotation has the following attributes:
 +
* <tt>name</tt> – The name of the generator must match the name of a <tt>GeneratedValue</tt> with its <tt>strategy</tt> attribute set to <tt>SEQUENCE</tt>.<br><br>You are required to specify the value of this attribute.
 +
* <tt>allocationSize</tt> – By default, EclipseLink persistence provider uses an allocation size of 50.<br><br>The value of this attribute must match the increment size on the database sequence object. If this allocation size does not match your application requirements or database performance parameters, set this attribute to the <tt>int</tt> value you want.<br><br>You are not required to specify the value of the <tt>allocationSize</tt> attribute.
 +
* <tt>initialValue</tt> – By default, EclipseLink persistence provider starts all primary key values from 0.<br>If this does not match an existing data model, set this attribute to the <tt>int</tt> value you want.<br><br>You are not required to specify the value of the <tt>initialValue</tt> attribute.
 +
* <tt>sequenceName</tt> – By default, EclipseLink persistence provider assigns a sequence name of its own creation.<br><br>The <tt>sequenceName</tt> defaults to the name of the <tt>SequenceGenerator</tt>. If you prefer to use an existing or predefined sequence, set <tt>sequenceName</tt> to the <tt>String</tt> name you want.<br><br>You are not required to specify the value of the <tt>sequenceName</tt> attribute.
 +
 +
This 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>.
 +
 +
<span id="Example 18-8"></span>
 +
''''' Usage of @SequenceGenerator'''''
 +
<source lang="java">
 +
@Entity
 +
public class Employee implements Serializable {
 +
    ...
 +
    @Id
 +
    @SequenceGenerator(name="Cust_Seq", allocationSize=25)
 +
    @GeneratorValue(strategy=SEQUENCE, generator="Cust_Seq")
 +
    @Column(name="CUST_ID")
 +
    public Long getId() {
 +
        return id;
 +
    }
 +
    ...
 +
}
 +
</source>
 +
 +
 +
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]].
  
  

Revision as of 12:47, 17 June 2010

@SequenceGenerator Annotation

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;
  • 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


The @SequenceGenerator annotation has the following attributes:

  • name – The name of the generator must match the name of a GeneratedValue with its strategy attribute set to SEQUENCE.

    You are required to specify the value of this attribute.
  • allocationSize – By default, EclipseLink persistence provider uses an allocation size of 50.

    The value of this attribute must match the increment size on the database sequence object. If this allocation size does not match your application requirements or database performance parameters, set this attribute to the int value you want.

    You are not required to specify the value of the allocationSize attribute.
  • initialValue – By default, EclipseLink persistence provider starts all primary key values from 0.
    If this does not match an existing data model, set this attribute to the int value you want.

    You are not required to specify the value of the initialValue attribute.
  • sequenceName – By default, EclipseLink persistence provider assigns a sequence name of its own creation.

    The sequenceName defaults to the name of the SequenceGenerator. If you prefer to use an existing or predefined sequence, set sequenceName to the String name you want.

    You are not required to specify the value of the sequenceName attribute.

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

Usage of @SequenceGenerator

 @Entity
 public class Employee implements Serializable {
     ...
     @Id
     @SequenceGenerator(name="Cust_Seq", allocationSize=25)
     @GeneratorValue(strategy=SEQUENCE, generator="Cust_Seq")
     @Column(name="CUST_ID")
     public Long getId() {
         return id;
     }
     ...
 }


For more information on the EclipseLink artifacts configured by these JPA metadata, refer to Descriptors and Sequencing.


Eclipselink-logo.gif
Version: 2.1.0
Other versions...