com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘portal.hibernate_sequence’ doesn’t
事件起因:
今天做一个小项目的时候用到了 Spring Data JPA,在调用JpaRepository的save()方法时一直报错,百思不得其解,辗转搜索,终于找到了解决方案
解决方法:
修改实体类的主键生成策略
@Entity
public class ProductCategory {
/** 类目id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
/** 类目名字. */
private String categoryName;
/** 类目编号. */
private Integer categoryType;
private Date createTime;
private Date updateTime;
// getter & setter...
}
将主键的生成策略改为 @GeneratedValue(strategy = GenerationType.IDENTITY), 默认是 @GeneratedValue(strategy = GenerationType.AUTO)
两者的区别:
AUTO: 当数据库中 不存在 这张表的时候可以用它建表的时候, 制定自增的方式, 存在的时候插入数据还用它就会出错了
IDENTIYY: 使用 MySQL 的自增字段
所以,错误的原因是我事先建好了表,而又使用了 GenerationType.AUTO 的生成策略