Creating an autogenerator column

Generally, we create a primary column with some autogenerated value. Hibernate allows us to create the same using code. Let's take a look at some methods to create a column with an autogenerated value.

How to do it…

We can create an autogenerated column in many ways, such as:

  • Using a default generation strategy
  • Using a sequence generator
  • Using a table generator

Default generation strategy

To use a default strategy for autogeneration, we will use the @GeneratedValue annotation, as follows:

@Id
@GeneratedValue
private long id;

Using the preceding code, hibernate will create a column with an autoincremental value.

By default, hibernate uses the GenerationType.AUTO strategy if no strategy is supplied; so, @GeneratedValue is equal to @GeneratedValue(strategy=GenerationType.AUTO).

Still, as it is database–specific, it's the responsibility of the database to provide a value for this column, and the same rule is applied for @GeneratedValue(strategy=GenerationType.IDENTITY).

Sequence generator

Here, we are using GenerationType.SEQUENCE in the @GeneratedValue annotation; let's take a look at how to do it.

Generally, the value for the column is provided by the database if it is a sequence.

We can create a sequence in the database, if it is supported by the database, and add the mapping in the Java code, as shown in the following code.

For example, our database sequence is created with the name "seq". We can use the same, as shown in the following code:

@Id
@SequenceGenerator(name="seq", sequenceName="DB_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private long id;

Here, the sequenceName = "DB_SEQ" value is a sequence name in the database, which is manually created by us.

Table generator

In a table generator, the value for the primary key column is stored in one table. Hibernate uses this table to get the next value for the primary key column in the particular class.

Let's take a look at how to do it using code:

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "gen_tbl")
@TableGenerator(name = "gen_tbl", table = "gen_table", pkColumnName = "pk", valueColumnName = "id", pkColumnValue = "employee0", initialValue = 0, allocationSize = 1)
private long id;

Here, we used the @TableGenerator annotation to define a table generator.

There's more…

Let's take a look at some attributes available in the @GeneratedValue and @Table Generator annotations.

Attributes available in the @GeneratedValue annotation

Let's consider some attributes available in the @GeneratedValue annotation.

Strategy

This attribute accepts the enum GenerationType value.

Four possible values available for enum GenerationType are as follows:

  • AUTO
  • IDENTITY
  • SEQUENCE
  • TABLE

Generator

This attribute accepts a string value; it's the name of the generator in GenerationType.SEQUENCE and GenerationType.TABLE.

Attributes available in @TableGenerator annotation

Now, we will consider some attributes of @TableGenerator.

name

The name attribute accepts a string value and defines a unique name for the table generator in a class. It is used in the @GeneratedValue annotation to provide a value to the generator attribute.

Here is an example:

name = "gen_tbl"

table

The table attribute accepts a string value. It is a new table name created by hibernate to contain the next value for the primary key column.

Here is an example:

table = "gen_table"

Here, hibernate will create a table with the name "gen_table".

pkColumnName

This attribute accepts a string value. It defines a column with the name "gen_table" in the table to store a key for the class.

Here is an example:

pkColumnName = "pk"

Here, hibernate will create a column with the name "pk" in the "gen_table" table.

valueColumnName

This attribute accepts a string value. It is another column used by hibernate to hold an actual value for the primary key column.

Here is an example:

valueColumnName = "id"

Here, hibernate will create a column with the name "id" in the "gen_table" table.

pkColumnValue

This is a static value for the particular class stored in the "pk" column. It is used to get a value for the primary key, which is stored in the "id" column against this value.

Here is an example:

pkColumnValue = "employee"

Here, hibernate will insert a row in "gen_table" with the "employee" value in the "pk" column and provide a value in the "id" column, which is equal to initialValue.

initialValue

This attribute defines an initial value for the primary column.

Here is an example:

initialValue = 0

allocationSize

This attribute defines the increment in a value for the primary key column.

Here is an example:

allocationSize = 1

Once the code is executed, hibernate will create the following table structure in the database:

Table: employee

id

1

Table: gen_table

pk

id

Employee

2

Here, the "gen_table" table shows a value in "id", which is 2, because we inserted a 1 record in employee, then hibernate updated the value of the "id" column for the "employee" key by allocationSize (here value 1).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.199.122