what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?
Let me answer this question:
First of all, using annotations as our configure method is just a convenient method instead of coping the endless XML configuration file.
The @Id
annotation 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.
In a Object Relational Mapping context, every object needs to have a unique identifier. You use the @Id
annotation to specify the primary key of an entity.
The @GeneratedValue
annotation is used to specify how the primary key should be generated. In your example you are using an Identity
strategy which
Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
There are other strategies, you can see more here.
Simply, @Id: This annotation specifies the primary key of the entity.
@GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used.
GenerationType enum defines four strategies:
1. Generation Type . TABLE,
2. Generation Type. SEQUENCE,
3. Generation Type. IDENTITY
4. Generation Type. AUTO
GenerationType.SEQUENCE
With this strategy, underlying persistence provider must use a database sequence to get the next unique primary key for the entities.
GenerationType.TABLE
With this strategy, underlying persistence provider must use a database table to generate/keep the next unique primary key for the entities.
GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
GenerationType.AUTO
This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.
Reference:- https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html