Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GenerationType.AUTO vs GenerationType.IDENTITY in hibernate

Currently, we are using MySQL as a database and we use

@Generated Value(strategy = GenerationType.IDENTITY)

It's working perfectly in certain situations we need to migrate our database to Oracle at that time it's not working properly. If anyone knows what's the actual difference is present behind this and how it's working?

like image 882
sethu palaniyappan Avatar asked Oct 13 '15 07:10

sethu palaniyappan


People also ask

What is GenerationType Auto?

GenerationType.AUTO sets @GeneratedValue automatic. If table has defined any default value or it has defined any auto increment in table then in that case we use. @GeneratedValue(strategy=GenerationType. AUTO)

What is GenerationType in hibernate?

SEQUENCE is the generation type recommended by the Hibernate documentation. The generated values are unique per sequence. If we don't specify a sequence name, Hibernate will reuse the same hibernate_sequence for different types.

What is GenerationType identity?

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

What is difference between generation type auto and identity?

AUTO: Hibernate selects the generation strategy based on the used dialect, IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.


1 Answers

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

like image 137
Neil Stockton Avatar answered Sep 22 '22 13:09

Neil Stockton