What Is the Use of Annotations @Id and @Generatedvalue(Strategy = Generationtype.Identity)? Why the Generationtype Is Identity

what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?

First of all, using annotations as our configure method is just a convenient method instead of coping the endless XML configuration file.

The @Idannotation is inherited from javax.persistence.Id, indicating the member field below is the primary key of current entity. Hence your Hibernate and spring framework as well as you can do some reflect works based on this annotation. for details please check javadoc for Id

The @GeneratedValue annotation is to configure the way of increment of the specified column(field). For example when using Mysql, you may specify auto_increment in the definition of table to make it self-incremental, and then use

@GeneratedValue(strategy = GenerationType.IDENTITY)

in the Java code to denote that you also acknowledged to use this database server side strategy. Also, you may change the value in this annotation to fit different requirements.

1. Define Sequence in database

For instance, Oracle has to use sequence as increment method, say we create a sequence in Oracle:

create sequence oracle_seq;

2. Refer the database sequence

Now that we have the sequence in database, but we need to establish the relation between Java and DB, by using @SequenceGenerator:

@SequenceGenerator(name="seq",sequenceName="oracle_seq")

sequenceName is the real name of a sequence in Oracle, name is what you want to call it in Java. You need to specify sequenceName if it is different from name, otherwise just use name. I usually ignore sequenceName to save my time.

3. Use sequence in Java

Finally, it is time to make use this sequence in Java. Just add @GeneratedValue:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")

The generator field refers to which sequence generator you want to use. Notice it is not the real sequence name in DB, but the name you specified in name field of SequenceGenerator.

4. Complete

So the complete version should be like this:

public class MyTable
{
@Id
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Integer pid;
}

Now start using these annotations to make your JavaWeb development easier.

Spring GeneratedValue annotation usage

Here is a good explanation of primary keys generation strategies

There are 4 options to generate primary keys


GenerationType.AUTO


The GenerationType.AUTO is the default generation type and lets the
persistence provider choose the generation strategy.

If you use Hibernate as your persistence provider, it selects a
generation strategy based on the database specific dialect. For most
popular databases, it selects GenerationType.SEQUENCE.

GenerationType.IDENTITY


The GenerationType.IDENTITY is the easiest to use but not the best one
from a performance point of view
. It relies on an auto-incremented
database column and lets the database generate a new value with each
insert operation. From a database point of view, this is very efficient because the auto-increment columns are highly optimized, and it doesn’t require any additional statements.

This approach has a significant drawback if you use Hibernate.
Hibernate requires a primary key value for each managed entity and
therefore has to perform the insert statement immediately. This
prevents it from using different optimization techniques like JDBC
batching.

GenerationType.SEQUENCE


The GenerationType.SEQUENCE uses a database sequence to generate
unique values. It requires additional select statements to get the
next value from a database sequence. But this has no performance
impact for most applications
.

If you don’t provide any additional information, Hibernate will
request the next value from its default sequence. You can change that
by referencing the name of a @SequenceGenerator in the generator
attribute of the @GeneratedValue annotation. The @SequenceGenerator
annotation lets you define the name of the generator, the name, and
schema of the database sequence and the allocation size of the
sequence.

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@SequenceGenerator(name="car_generator", sequenceName = "car_seq", allocationSize=50)
private Long id;

(Side note: Usually prefer Long for ids instead of Integer so you're less likely to run out)

GenerationType.TABLE


The GenerationType.TABLE gets only rarely used nowadays. It simulates
a sequence by storing and updating its current value in a database
table which requires the use of pessimistic locks which put all
transactions into a sequential order. This slows down your
application, and you should, therefore, prefer the
GenerationType.SEQUENCE
, if your database supports sequences, which
most popular databases do.

If the database table column is set with auto_increment then why are we using @generatedValue

@GeneratedValue is used to inform JPA provider that particular field should receive generated value. Then based on GenerationType passed to @GeneratedValue annotation, provider will decide how to assign values to primary keys, for example by delegating it to some database mechanism.

For example oracle databases require sequences to get generated values for primary keys so in case of using oracle database you would pass GenerationType.SEQUENCE to @GeneratedValue, and you will also have to create such sequence.

In case of MySQL databases you would pass GenerationType.IDENTITY and underneath it would use AUTO_INCREMENT of primary key column in your MySQL table.

As documentation for GenerationType.IDENTITY says :

IDENTITY
Indicates that the persistence provider must assign primary keys for the entity using a database identity column.

We need all those annotations because JPA is just an abstraction for working with relational databases and it can work with different databases which use different mechanism undearneath. Hibernate is just one of implementations of JPA.



Related Topics



Leave a reply



Submit