为什么在使用策略GenerationType.IDENTITY时Hibernate尝试访问hibernate_sequence?

时间:2018-08-18 13:54:02

标签: java mysql hibernate spring-boot

在我的项目中,我将MySQL与Hibernate和JPA结合使用。我的实体都用@ID @GeneratedValue(strategy = GenerationType.IDENTITY)注释。

尽管如此,当在数据库中插入新对象时,Hibernate会尝试从表hibernate_sequence中选择下一个ID-显然会失败。

我检查了整个项目,是否偶然在某个实体上使用了不同的发电类型,但事实并非如此。还有什么可能导致此问题?

这是我用来创建表的SQL:

create table versioned_string_attribute (
  id int primary key auto_increment not null,
  timestamp timestamp,
  value text
);


create table backend_address (
  id int primary key auto_increment not null,
  street_id int references versioned_string_attribute(id),
  house_number_id int references versioned_string_attribute(id),
  zip_code_id int references versioned_string_attribute(id),
  town_id int references versioned_string_attribute(id),
  addition_id int references versioned_string_attribute(id)
);

这是我的application.properties

spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.driverClassName=com.mysql.jdbc.Driver

1 个答案:

答案 0 :(得分:0)

从docs(https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#common-application-properties)中获取属性:

spring.jpa.hibernate.use-new-id-generator-mappings =#是否对AUTO,TABLE和SEQUENCE使用Hibernate更新的IdentifierGenerator。

所以我通过添加spring.jpa.hibernate.use-new-id-generator-mappings=false

解决了这个问题

有关此的更多详细信息:https://vladmihalcea.com/from-jpa-to-hibernates-legacy-and-enhanced-identifier-generators/