使用 SpringDataJPA 总是创建 hibernate_sequence 表

异信息:

2018-05-03 16:29:25.147 [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

2018-05-03 16:29:32.061 [main] DEBUG org.hibernate.SQL - create table hibernate_sequence (next_val bigint) type=MyISAM
Hibernate: create table hibernate_sequence (next_val bigint) type=MyISAM
2018-05-03 16:29:32.127 [main] WARN  o.h.t.schema.internal.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement

当使用@GeneratedValue时

@Id
@GeneratedValue
private Long id;
总是遇到以上错误;

处理方法:

1. @GeneratedValue 改用 

@GeneratedValue(strategy = GenerationType.IDENTITY)

2. JPA 配置: 

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

错误分析:注解 @GeneratedValue 有一个属性strategy 是用来定义主键生成策略的;

GenerationType.AUTO即是四种策略之一,把主键生成策略交给持久化引擎(persistence engine),持久化引擎会根据数据库在以上三种主键生成策略中选择其中一种。因为JPA默认主键生成策略为 GenerationType,  所以@GeneratedValue  等价于 @GeneratedValue(strategy = GenerationType.AUTO);

@GeneratedValue(strategy = GenerationType.IDENTITY)


则是主键自增长,即mysql中的 auto_increment.

发布了476 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_37769323/article/details/104880962