Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and how hibernate_sequence is using for id generatior

I'm faced out an issue with ID generation for my entities generated by JHipster:

  1. Dev environment on H2 database
  2. Prod environment on Postgres
  3. I have one entity "station" with two fields "id" and "name"
  4. Creating a liquibase script which imports dictionary in "station" table like INSERT INTO station (name) VALUES ('Adygeya') without ID definition
  5. Trying to add station on dev environment - OK
  6. Trying to add station on prod - Hibernate tries to add new station with duplicated ID

WHY?

My research was shown that only for postgres and oracle in initial scheme jhipster created a new sequince "hibernate_sequence" which is used for new entities creation.

So I'm fixed out this wrong behaivour by adding specific sequence name for my entity ID generation rule

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "station_gen")
@SequenceGenerator(name = "station_gen", sequenceName = "station_id_seq")
private Long id;

Now I have just 3 questions:

  1. Why JHipster using one sequince for all tables for postgres and oracle?
  2. Where is it configured?
  3. What am I doing wrong?
like image 245
Sergey Babinsky Avatar asked Jan 05 '17 00:01

Sergey Babinsky


People also ask

What is hibernate_sequence used for?

Notice that, by default, the hibernate_sequence is used for all entities using the SEQUENCE identifier generation strategy without an explicit database sequence name. Notice that the hibernate_sequence was called five times since, by default, no sequence call optimizer is used.

How does GenerationType auto work?

GenerationType.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.

What is the use of @SequenceGenerator?

Annotation Type SequenceGenerator. Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property.

How does @GeneratedValue work?

The @GeneratedValue annotation tells the ORM how to figure out the value of that field. Typcial generators you will run into. It is possible to develop custom generator. The interaction with the database will depend on generation strategy.


1 Answers

Will post here my answer wich was found with help of @GaëlMarziou and @Julien Dubois.

Thats because of history and default behaivour of Hibernate hilo algorithm with GenerationType.AUTO. MySQL doesn't support sequencies so JHipster needs to use this stupid algorithm

JHipster team has found right solution wich will fix my problem. The solution is to use GenerationType.SEQUENCE for all DB but use GenerationType.IDENTITY for MySql. All the details in this commit https://github.com/jhipster/generator-jhipster/commit/4516b4ff4d49a96a75fd963b0c7667f198bd9b79

This way I will configure my entities now too.

like image 53
Sergey Babinsky Avatar answered Nov 09 '22 23:11

Sergey Babinsky