com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'portal.hibernate_sequence' doesn't exist, 谈谈主键自增的方式
最近几天几天做项目用到了Spring Data JPA,确实是个好东西,省了很多力气。但是由于刚开始用,也遇到不少头疼的问题,如下,调用JpaRepository接口的save方法保存一个对象到数据库中的时候出错:
ERROR:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'portal.hibernate_sequence' doesn't exist, wqee
对这个问题可以说是几经磨难,一直想不通为何会提示sequence不存在,数据库明明用的是Mysql, 不可能用到序列啊
网上百度了好久,答案最多的是说: 数据库的方言配置出错,用啥数据库,配啥方言.
想想肯定不对, 我的框架是从别的项目改过来的,配置肯定没问题.
纠结了两天,最后忍不住问项目经理,经理过来瞄了一眼就说: 主键自增策略有问题
源码如下:
- package com.sanss.portal.model;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="liuliangbaos")
- public class Liuliangbaos {
- private int id;
- private String name;
- private String zifeimiaosu;
- private String canpinmiaosu;
- private String created_at;
- private String updated_at;
- private String bianhao;
- private String shiyongyouxiaoqi;
- private String rongliang;
- private String shiyongfanwei;
- public Liuliangbaos() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Liuliangbaos(int id, String name, String zifeimiaosu, String canpinmiaosu, String created_at,
- String updated_at, String bianhao, String shiyongyouxiaoqi, String rongliang, String shiyongfanwei) {
- super();
- this.id = id;
- this.name = name;
- this.zifeimiaosu = zifeimiaosu;
- this.canpinmiaosu = canpinmiaosu;
- this.created_at = created_at;
- this.updated_at = updated_at;
- this.bianhao = bianhao;
- this.shiyongyouxiaoqi = shiyongyouxiaoqi;
- this.rongliang = rongliang;
- this.shiyongfanwei = shiyongfanwei;
- }
- @Id
- 42 @GeneratedValue(strategy=GenerationType.AUTO)
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getZifeimiaosu() {
- return zifeimiaosu;
- }
- public void setZifeimiaosu(String zifeimiaosu) {
- this.zifeimiaosu = zifeimiaosu;
- }
- public String getCanpinmiaosu() {
- return canpinmiaosu;
- }
- public void setCanpinmiaosu(String canpinmiaosu) {
- this.canpinmiaosu = canpinmiaosu;
- }
- public String getCreated_at() {
- return created_at;
- }
- public void setCreated_at(String created_at) {
- this.created_at = created_at;
- }
- public String getUpdated_at() {
- return updated_at;
- }
- public void setUpdated_at(String updated_at) {
- this.updated_at = updated_at;
- }
- public String getBianhao() {
- return bianhao;
- }
- public void setBianhao(String bianhao) {
- this.bianhao = bianhao;
- }
- public String getShiyongyouxiaoqi() {
- return shiyongyouxiaoqi;
- }
- public void setShiyongyouxiaoqi(String shiyongyouxiaoqi) {
- this.shiyongyouxiaoqi = shiyongyouxiaoqi;
- }
- public String getRongliang() {
- return rongliang;
- }
- public void setRongliang(String rongliang) {
- this.rongliang = rongliang;
- }
- public String getShiyongfanwei() {
- return shiyongfanwei;
- }
- public void setShiyongfanwei(String shiyongfanwei) {
- this.shiyongfanwei = shiyongfanwei;
- }
- }
果然,改成 identity 就OK 了!!! 姜还是老的辣啊
那么问题来了,mysql数据库怎么可以用SQL的主键自增方式 identity 呢??? 为嘛不能用自己的 auto ???
查了一下资料,大致解释如下:
- Hibernate 能够出色地自动生成主键。Hibernate/EBJ 3 注释也可以为主键的自动生成提供丰富的支持,允许实现各种策略。
- 其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出.
- JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
- TABLE:使用一个特定的数据库表格来保存主键。
- SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
- IDENTITY:主键由数据库自动生成(主要是自动增长型)
- AUTO:主键由程序控制。
- 在指定主键时,如果不指定主键生成策略,默认为AUTO。
- @Id
- 相当于
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- identity:
- 使用SQL Server 和 MySQL 的自增字段,这个方法不能放到 Oracle 中,Oracle 不支持自增字段,要设定sequence(MySQL 和 SQL Server 中很常用)。
- Oracle就要采用sequence了.
- 同时,也可采用uuid,native等其它策略.(相关用法,上网查询)
也就是说:
auto: 当数据库中 不存在 这张表的时候可以用它建表的时候, 制定自增的方式, 存在的时候插入数据还用它就会出错了
identity: 使用SQL Server 和 MySQL 的自增字段
总结一下,希望对大家有用!
(PS:感谢经理,大牛就是大牛啊,一眼就解决了我两天的纠结!)
详情理解如下:
http://www.cnblogs.com/younggun/archive/2013/05/19/3086659.html
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'portal.hibernate_sequence' doesn't exist, 谈谈主键自增的方式的更多相关文章
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jeewx.weixin_account_user_relation' doesn't exist
[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------- ...
- [jnhs]hibernate只能创建一张/表不创建表com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'kaihu.t_client_info' doesn't exist和org.hibernate.exception.SQLGrammarException: could not execute statement
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'kaihu.t_client_info' doesn't exist ...
- 将windows上面的项目拷贝到Linux环境下报错不能够找到对应的表com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'puyang.ServiceType' doesn't exist
将一模一样的项目从win迁移到到linux上报错: 一开始还是以为是linux不能识别hql语句,查找资料发现是因为Liunx服务器上mysql是区分大小写的,而本地是不区分的如:代码是这样写的 @E ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'easylabdb.userInfo' doesn't exist
这个问题主要是说,你查找的表不存在,但是,事实上我这个表示存在的,会产生这个问题的原因是,我这个表的大小写拼写方式跟sql语句中的大小写不一样,这时就要设置数据库不区分大小写 找到mysql的配置文件 ...
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'zhongfucheng.user' does
编写第一个Hibernate程序的时候,就发现出现了错误 Exception in thread "main" org.hibernate.exception.SQLGrammar ...
- 【异常】ERROR main:com.cloudera.enterprise.dbutil.SqlFileRunner: Exception while executing ddl scripts. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'ROLES' already exists
1 详细异常 2019-10-11 10:33:55,865 INFO main:com.cloudera.server.cmf.Main: ============================= ...
- 错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hdjyproj.t_userinfo' do ...
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: REFERENCES command denied to user 'nali'@'localhost' for table 'dbs'
按照教程 Install hive on Mac with Homebrew,在 mac 上安装 Hive 时, 最后执行 hive 命令后,出现错误: Exception in thread &qu ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’
Linux环境 Mysql+Hibernate command denied to user 错误 错误信息 如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...
随机推荐
- WEB前端:浏览器(IE+Chrome+Firefox)常见兼容问题处理--02
兼容问题目录 8.IE6不支持固定定位 9.IE6下前面元素浮动,后面元素不浮动后他们之间会有间隙 10.IE6下双边距问题 11.IE67下父级有边框,子级有margin的话会不起作用 12.IE6 ...
- 一天搞定CSS:定位position--17
1.定位取值概览 2.相对定位relative <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- vue使用Axios做ajax请求
vue2.0之后,就不再对vue-resource更新,而是推荐使用axios 1. 安装 axios $ npm install axios 或 $ bower install axios 2. 在 ...
- Coursera 机器学习笔记(二)
主要为第三周课程内容:逻辑回归与正则化 逻辑回归(Logistic Regression) 一.逻辑回归模型引入 分类问题是指尝试预测的是结果是否属于某一个类. 维基百科的定义为:根据已知训练区提供的 ...
- 邪恶改装:TPYBoard制作廉价WIFI干扰器
转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 0X01 引言 想不想搞个WIFI干扰器?网上搜集了一下资料,发现用esp8266可以实现简单的干扰功能,包括断网. ...
- react-native学习(RN)--之Window环境下搭建环境配置
react-native以后会更火的,自从2015年facebook开源了Android 一.安装java 二.安装Android Studio 三.安装react-native需要的Android ...
- Spring学习(17)--- 三种装配Bean方式比较
基于XML配置 基于注解配置 基于Java类配置 Bean定义 <bean id="..." class="..." /> @Compone ...
- .NET 随记
1. goto 常用于 switch语句中2. 字符串相加用 StringBuilder的Append()方法性能好3. str.Trim(',') 清除字符串后的","4. st ...
- LeetCode 206 单链表翻转
https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -& ...
- MongoDB--MapReduce分布统计s
MapReduce Mapreduce:要操作的目标集合 Map:映射函数(生成键值对序列,作为reduce函数参数) //传入分组的key和需要统计的值 Reduce:统计函数 //格式化返回的参数 ...