Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has Spring-boot changed the way auto-increment of ids works through @GeneratedValue?

Spring-Boot 2.0.0 seems to have modified the way Hibernate is auto configured.

Let's suppose two simple and independent JPA entities:

@Entity
class Car {
   @Id
   @GeneratedValue
   private long id;
   //....
} 

@Entity
class Airplane {
   @Id
   @GeneratedValue
   private long id;
   //....
}

Prior, using Spring-Boot 1.5.10, I was able to generate separate sequences of auto-increments, meaning that I can get a Car with 1 as primary key and an Airplane with 1 as primary key too. No correlation between them, e.g no shared sequence.

Now, with 2.0.0, when I sequentially create a very first Car then a very first Airplane, the car gets 1 as id and airplane gets 2.

It seems that he has to deal with the GeneratedType.AUTO, that is the "used by default" specified within the @GeneratedValue annotation source.
However, my reasoning seems to stop here since GeneratedType.AUTO was also set as default with the 1.5.10.

A simple workaround to fulfil my expectation is to specify the IDENTITY strategy type of generation like so:

@Entity
class Car {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   //....
} 

@Entity
class Airplane {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   //....
}

I can't figure out an explanation of this behavior.

What has Spring-boot 2.0.0 changed, explaining this scenario?

like image 645
Florian Laforest Avatar asked Mar 12 '18 17:03

Florian Laforest


People also ask

What does @ID do in spring boot?

The @Id annotation specifies the primary key of an entity and the @GeneratedValue provides for the specification of generation strategies for the values of primary keys.

What is @GeneratedValue strategy GenerationType Auto?

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.

What does @data do in spring boot?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...

How auto increment is primary key in hibernate?

If you want to use this strategy, you have to annotate the primary key attribute @Id and with the @GeneratedValue annotation and set the strategy attribute to GenerationType. IDENTITY. If you now persist a new Author entity, Hibernate will use the auto-incremented database column to generate the primary key value.


2 Answers

Spring Boot 2.0 uses Hibernate 5.2 (https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes).
Hibernate changes its GeneratedType.AUTO strategy since 5.2. Any database that does not support sequences natively (e.g. MySQL), they use the TABLE generator instead of IDENTITY. (https://hibernate.atlassian.net/browse/HHH-11014)

That's why GeneratedType.AUTO does not work as you expected.

like image 64
Min Hyoung Hong Avatar answered Nov 09 '22 22:11

Min Hyoung Hong


You can use

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

to use MySQL autoincrement.

like image 43
Mojtaba Shayegh Avatar answered Nov 09 '22 23:11

Mojtaba Shayegh