Skip to main content
Jump to: navigation, search

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

Line 128: Line 128:
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|@GeneratedValue]]
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/GeneratedValue|@GeneratedValue]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator|@SequenceGenerator]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator|@SequenceGenerator]]
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities|Entity]]
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Entity]]
|version=2.2.0 DRAFT}}
|version=2.2.0 DRAFT}}

Revision as of 11:02, 14 June 2011

EclipseLink JPA

Mailing ListForumsIRCmattermost
OpenHelp WantedBug Day
Browse Source

Elug api package icon.png Key API


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

  • change the name of the primary key generator's table, because the name is awkward, a reserved word, incompatible with a preexisting data model, or invalid as a table name in your database;
  • 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 primary key values has already been assigned or reserved);
  • configure the primary key generator's table with a specific catalog or schema;
  • configure a unique constraint on one or more columns of the primary key generator's table;
Elug javaspec icon.gif

For more information, see Section 11.1.46 "TableGenerator Annotation" in the JPA Specification.

@TableGenerator Attributes
Attribute Description Default Required?
name The name of the generator must match the name of a GeneratedValue with its strategy attribute set to TABLE. The scope of the generator name is global to the persistence unit (across all generator types). Yes
allocationSize An int value to match your your application requirements or database performance parameters. 50 No
catalog A String catalog name. Default catalog for database No
initialValue An int initial primary key value. 0 No
pkColumnName A String name for the primary key column in the generator table. SEQ_NAME No
pkColumnValue String primary key value for the primary key column in the generator table. No
schema The String name of the schema. Default schema of the database No
table A String name of the table in which to store the generated ID values. SEQUENCE No
uniqueConstraints By default, EclipseLink persistence provider assumes that none of the columns in the primary key generator table have unique constraints.
If unique constraints do apply to one or more columns in this table, set the value of this attribute to an array of one or more UniqueConstraint instances.
Elug javaspec icon.gif

For more information, see Section 11.1.49 "UniqueConstraint Annotation" in the JPA Specification.

No additional constraints No
valueColumnName A String name of the column that stores the generated ID values. SEQ_COUNT No

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

Example: Using @TableGenerator
public class Employee implements Serializable {
    @TableGenerator(name="Emp_Gen", allocationSize=1)
    @GeneratorValue(strategy=TABLE, generator="Emp_Gen")
    public Long getId() {
        return id;
Example: Using <table-generator>
<entity class="Employee">
        <id name="id">
            <generated-value generator="Emp_Gen" strategy="TABLE"/>
            <table-generator name="Emp_Gen" allocationSize="1"/>
            <column name="EMP_ID"/>

Every table that you use for id generation should have two columns – if there are more columns, only two will be used. The first column is of a string type and is used to identify the particular generator sequence. It is the primary key for all of the generators in the table. The name of this column is specified by the pkColumnName attribute. The second column is of an integer type and stores the actual id sequence that is being generated. The value stored in this column is the last identifier that was allocated in the sequence. The name of this column is specified by the valueColumnName attribute.

Each defined generator represents a row in the table. The name of the generator becomes the value stored in the pkColumnName column for that row and is used by EclipseLink persistence provider to look up the generator to obtain its last allocated value.

Version: 2.2.0 DRAFT
Other versions...

Back to the top