Spring-boot是否通过@GeneratedValue更改了ID自动递增的工作方式? [英] Has Spring-boot changed the way auto-increment of ids works through @GeneratedValue?

查看:305
本文介绍了Spring-boot是否通过@GeneratedValue更改了ID自动递增的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring-Boot 2.0.0 似乎已经修改了自动配置 Hibernate 的方式.

让我们假设两个简单且独立的JPA实体:

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

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

之前,使用Spring-Boot 1.5.10 ,我能够生成单独的自动增量序列,这意味着我可以使用 1 获得Car作为主键,也将 1 作为主键的Airplane. 它们之间没有相关性,例如没有共享序列.

现在,使用 2.0.0 ,当我依次创建第一个Car然后是第一个Airplane时,汽车的ID和飞机将获得 1 得到 2 .

似乎他必须处理GeneratedType.AUTO,即@GeneratedValue注释源中指定的默认使用".
但是,由于GeneratedType.AUTO 1.5.10 中也被设置为默认值,因此我的推理到此为止.

要满足我的期望,一个简单的解决方法是指定生成的IDENTITY策略类型,如下所示:

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

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

我不知道这种行为的解释.

Spring-boot 2.0.0 发生了什么变化,解释了这种情况?

解决方案

Spring Boot 2.0使用Hibernate 5.2( https://hibernate.atlassian.net/browse/HHH-11014 )

这就是为什么GeneratedType.AUTO不能按预期工作的原因.

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?

解决方案

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.

这篇关于Spring-boot是否通过@GeneratedValue更改了ID自动递增的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆