GenerationType.AUTO vs GenerationType.IDENTITY in hibernate
How could it "work properly" (you don't define basic info like what you mean by that) with Oracle ? I don't see the relevance of AUTO
to your question - that simply lets an implementation choose what it wants to use.
"IDENTITY
" (as per JPA javadocs and spec - what you should be referring to) means autoincrement. There is no such concept in Oracle, yet there is in MySQL, SQLServer and a few others. I would expect any decent JPA implementation to flag an error when even trying such a thing.
Oracle would allow "SEQUENCE
", or "TABLE
" strategies to be used however
Hibernate GenerationType.IDENTITY vs GenerationType.SEQUENCE
As it's stated in the hibernate documentation:
It is important to realize that using
IDENTITY
columns imposes a runtime behavior where the entity row must be physically inserted prior to the identifier value being known.This can mess up extended persistence contexts (long conversations). Because of the runtime imposition/inconsistency, Hibernate suggests other forms of identifier value generation be used (e.g. SEQUENCE).
There is yet another important runtime impact of choosing IDENTITY generation: Hibernate will not be able to batch INSERT statements for the entities using the IDENTITY generation.
The importance of this depends on the application-specific use cases. If the application is not usually creating many new instances of a given entity type using the IDENTITY generator, then this limitation will be less important since batching would not have been very helpful anyway.
Difference between Hibernate Automatic value generation strategies?
This is like following:
AUTO Indicates that the persistence provider should pick an appropriate strategy for the particular database.
IDENTITY
Indicates that the persistence provider must assign primary keys for the entity using database identity column.SEQUENCE
Indicates that the persistence provider must assign primary keys for the entity using database sequence column.TABLE
Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
Refer to the API here http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html
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 @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.
Hibernate 5 ID AUTO Generation Type for Oracle as Sequence and MySQL as Identity
I almost had it in the question, but in case somebody falls in the same situation: calling the generator HIBERNATE_SEQUENCE will make it backwards compatible with Oracle and PostgreSQL (SQL Server will continue using IDENTITY).
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "HIBERNATE_SEQUENCE")
@GenericGenerator(name = "HIBERNATE_SEQUENCE", strategy = "native")
protected Long id;
What strategy is actually selected for "GenerationType.AUTO" for the major databases?
This link is about Java Persistence API and seems regularly updated.
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing
Identity sequencing
Identity sequencing uses special IDENTITY columns in the database to
allow the database to automatically assign an id to the object when
its row is inserted. Identity columns are supported in many databases,
such as MySQL, DB2, SQL Server, Sybase, and PostgreSQL. Oracle does
not support IDENTITY columns but its is possible to simulate them
using sequence objects and triggers.Sequence objects
Sequence objects use special database objects to generate ids.
Sequence objects are only supported in some databases, such as Oracle,
DB2, and Postgres. Usually, a SEQUENCE object has a name, an
INCREMENT, and other database object settings. Each time the
.NEXTVAL is selected the sequence is incremented by the
INCREMENT.
Edit
If the database such as DB2 supports both IDENTITY columns and Sequences hibernate chooses Identity columns, see Dialect:
public Class getNativeIdentifierGeneratorClass() {
if ( supportsIdentityColumns() ) {
return IdentityGenerator.class;
}
else if ( supportsSequences() ) {
return SequenceGenerator.class;
}
else {
return TableHiLoGenerator.class;
}
}
You can check what is returned from supportsIdentityColumns() and supportsSequences() for each database by looking at the relevant dialect in the org.hibernate.dialect
package.
Related Topics
Json Parsing Using Gson For Java
Waiting For Multiple Swingworkers
Java Security: Illegal Key Size or Default Parameters
Persistentobjectexception: Detached Entity Passed to Persist Thrown by JPA and Hibernate
How to Use Utf-8 in Resource Properties With Resourcebundle
Instantiating a Generic Class in Java
Swing: Link Toggle Buttons Together With a Button Group, Along With Corresponding Menu Items
How to Print Color in Console Using System.Out.Println
How to Tell Gradle to Use Specific Jdk Version
How Can a Java Program Get Its Own Process Id
How to Read and Write Excel File
How to Sort by Two Fields in Java
@Requestparam VS @Pathvariable