Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/TableGenerator

@TableGenerator Annotation

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 9.1.38 "TableGenerator Annotation" in the JPA Specification.

The @TableGenerator annotation has the following attributes:

  • 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).

    You are required to specify the value of this attribute.
  • allocationSize – By default, EclipseLink persistence provider uses an allocation size of 50.
    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.
  • catalog – By default, EclipseLink persistence provider uses whatever the default catalog is for your database.
    If the default catalog is inappropriate for your application, set the value of this attribute to the String catalog name to use.

    You are not required to specify the value of the catalog 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.
  • pkColumnName – By default, EclipseLink persistence provider supplies a name for the primary key column in the generator table: "SEQ_NAME".
    If this name is inappropriate for your application, set the value of this attribute to the String name you want.

    You are not required to specify the value of the pkColumnName attribute.
  • pkColumnValue – By default, EclipseLink persistence provider supplies a suitable primary key value for the primary key column in the generator table: TableGenereator.name.
    If this value is inappropriate for your application, set the value of this attribute to the String value you want.

    You are not required to specify the value of the pkColumnValue attribute.
  • schema – By default, EclipseLink persistence provider uses whatever the default schema is for your database.
    If this value is inappropriate for your application, set the value of this attribute to the String schema name you choose.

    You are not required to specify the value of the schema attribute.
  • table – By default, EclipseLink persistence provider supplies a suitable name for the table that stores the generated id values: "SEQUENCE".
    If this value is inappropriate for your application, set the value of this attribute to the String table name you want.

    You are not required to specify the value of the table attribute.
  • 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 9.1.4 "UniqueConstraint Annotation" in the JPA Specification.

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

  • valueColumnName – By default, EclipseLink persistence provider supplies a suitable name for the column that stores the generated id values: "SEQ_COUNT".
    If the default column name is inappropriate for your application, set the value of this attribute to the String column name you want.

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

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

Usage of @TableGenerator

 @Entity
 public class Employee implements Serializable {
     ...
     @Id
     @TableGenerator(name="Emp_Gen", allocationSize=1)
     @GeneratorValue(strategy=TABLE, generator="Emp_Gen")
     @Column(name="CUST_ID")
     public Long getId() {
         return 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.



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