mkazma
mkazma

Reputation: 582

why generationtype.sequence in mysql doesn't work?

i am developing an application using hibernate 4.1, zk 6.5 and mySql 9.3, I tried using generationtype.sequence in my code but unfortunately it returned an error, after doing some research on the internet I found out that MySQL doesn't support sequence so instead I used @GeneratedValue(strategy = GenerationType.IDENTITY) and it fixed my problem! but I was wondering why sequence doesn't work with mySql ?

Upvotes: 2

Views: 4601

Answers (1)

granadaCoder
granadaCoder

Reputation: 27904

I'm leaving an answer, so this question can be removed from the "unanswered" SOF category. KevinB, you can leave an answer, and I'll upvote it. I just want this question to be closed out...more than (me) "getting points".

But here is a concrete answer and reference.

Short Version:

MySql does not have "true" sequences. It uses a Sequence-via-a-Table workaround...that is a bottleneck if you're trying to do alot of JPA batching.

https://thorben-janssen.com/5-things-you-need-to-know-when-using-hibernate-with-mysql/

  1. Mappings: Primary Keys The efficient handling and creation of primary keys are a basic but one of the most important parts of an application.

The @GeneratedValue annotation of the JPA specification allows you to define the strategy you want to use to create unique primary key values. You can choose between SEQUENCE, IDENTITY, TABLE, and AUTO.

In general, I recommend using the SEQUENCE strategy because it allows Hibernate to use JDBC batching and other optimization strategies that require the delayed execution of SQL INSERT statements.

But you can’t use this strategy with a MySQL database. It requires a database sequence, and MySQL doesn’t support this feature.

Upvotes: 2

Related Questions