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 ...
随机推荐
- Deep Learning in R
Introduction Deep learning is a recent trend in machine learning that models highly non-linear repre ...
- haproxy-代码阅读-内存管理
haproxy内存池概述 内存池按照类型分类,每个类型的内存池都有一个名字,用链表记录空闲的内存块,每个内存块大小相等,并按照16字节对齐. haporxy用pool_head 结构记录内存池 str ...
- Exploit-Exercises nebule 旅行日志(一)
exploit-exercises.com provides a variety of virtual machines, documentation and challenges that can ...
- 微信小程序 获取OpenId
微信小程序 官方API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/ 首先 以下代码是 页面加载请求用户 是否同意授权 同意之后 用code 访问 微信 ...
- JAVAEE学习——struts2_01:简介、搭建、架构、配置、action类详解和练习:客户列表
一.struts2是什么 1.概念 2.struts2使用优势以及历史 二.搭建struts2框架 1.导包 (解压缩)struts2-blank.war就会看到 2.书写Action类 public ...
- 数据库并行读取和写入(Python实现)
这篇主要记录一下如何实现对数据库的并行运算来节省代码运行时间.语言是Python,其他语言思路一样. 前言 一共23w条数据,是之前通过自然语言分析处理过的数据,附一张截图: 要实现对news主体的读 ...
- 在.net下打造mongoDb基于官方驱动最新版本
还是一如既往先把结构图放出来,上上个版本添加了redis的缓存,但是不满足我的需求,因为公司有项目要求是分布式所以呢,这里我就增加了mongoDb进行缓存分布式,好了先看结构图. 总的来说比较蛋疼,因 ...
- Xamarin控件使用之GridView
[Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启 ...
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置.大小.粗度.翻转.平滑等操作 javaCV图像处理之2:实时视频添加图片水 ...
- Work Time Manager【开源项目】- 创建自己日志组件 2.0重构
Hello all , 我又回来了 这次我们真是开始来聊聊开源项目里,小而有用的模块或者组件的开发思想. 同时,软件已经更新到1.60的版本了,支持新用户注册,可以不再使用统一的test账户了. 您可 ...