Learn about @GeneratedValue annotation in JPA – Part 2

This tutorial is continued part 1.

The next strategy we want to talk is the GenerationType.SEQUENCE strategy

You declare this strategy in the Clazz entity as follows:

Similar to GenerationType.TABLE strategy, GenerationType.SEQUENCE strategy also depends on the value of “hibernate.id.new.generator_mappings” property in the JPA configuration file that is true or false.

If the value of this property is false, the generated values for the primary key column will be based on the database sequence of the database that we are using. On the other hand, you can imagine the database sequence is the object that holds the information of the primary key columns in the tables. When we insert a new record, this sequence will return the value of the primary key column for the record, then we can insert this record. Currently, MySQL does not support database sequence so in this case, when I run the example, I have the following error:

If the value of the “hibernate.id.new_generator_mappings” property is true, Hibernate will use a default table named hibernate_sequence, noting that sequence without “s” character. The purpose of this table is to store information for generating the value of the primary key column in the clazz table. The structure of this table is as follows:

At this point, if you run your example, you will encounter the following error:

The reason is that in order to use the hibernate_sequence table, we need to insert the initial value for the Clazz entity with the sequence_name is Clazz and next_val is 1.

Run the example again, you will see the results as follows:

Learn about @GeneratedValue annotation in JPA

As you can see, here I have run 2 times and each time the value of the next_val column in hibernate_sequence table increases to 1 unit. At first, when no record is available, a new record will be inserted into the hibernate_sequence table with the value next_val of 2. The value of the primary key column in the clazz table also increases to 1 unit respectively.

We can also change the default Hibernate table for this strategy using the @SequenceGenerator annotation and declare it like this:

In the @SequenceGenerator annotation, the sequenceName attribute with the name of the table we want to use, the structure of this table is similar to the Hibernate default hibernate_sequence table:

allocationSize is similar to GenerationType.TABLE strategy.

The important part is that the value of the name attribute in the @SequenceGenerator annotation will be the value of the generator attribute in the @GeneratedValue annotation.

Result:

Learn about @GeneratedValue annotation in JPA

Next, we’ll talk about strategy GenerationType.IDENTITY

This is the convenient and easiest strategy. You declare this strategy in the Clazz entity as follows:

This strategy is used to take advantage of some types of databases that support self-generated values for the primary key column. For example, in this tutorial, we define the primary key id of the table clazz with AUTO_INCREMENT. With this definition, whenever we add a new record to the clazz table, MySQL will help us to generate new values for the primary key column without having to do anything.

No need to define another table so I strongly encourage you to use this strategy if your database which is using, support 😀

The final strategy and also the default strategy of the @GeneratedValue annotation is GenerationType.AUTO

Since it is the default strategy, you do not need to declare it explicitly.

If you want, you can also declare explicitly as follows:

Generally, we will use GenerationType.AUTO when we need to generate the value for the primary key column without having to consider how the value of the primary key column will be generated. JPA can then choose any one of the 3 strategies we have discussed above.


Add Comment