最近几天几天做项目用到了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, 谈谈主键自增的方式的更多相关文章

  1. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jeewx.weixin_account_user_relation' doesn't exist

    [INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------- ...

  2. [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 ...

  3. 将windows上面的项目拷贝到Linux环境下报错不能够找到对应的表com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'puyang.ServiceType' doesn't exist

    将一模一样的项目从win迁移到到linux上报错: 一开始还是以为是linux不能识别hql语句,查找资料发现是因为Liunx服务器上mysql是区分大小写的,而本地是不区分的如:代码是这样写的 @E ...

  4. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'easylabdb.userInfo' doesn't exist

    这个问题主要是说,你查找的表不存在,但是,事实上我这个表示存在的,会产生这个问题的原因是,我这个表的大小写拼写方式跟sql语句中的大小写不一样,这时就要设置数据库不区分大小写 找到mysql的配置文件 ...

  5. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'zhongfucheng.user' does

    编写第一个Hibernate程序的时候,就发现出现了错误 Exception in thread "main" org.hibernate.exception.SQLGrammar ...

  6. 【异常】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: ============================= ...

  7. 错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hdjyproj.t_userinfo' do ...

  8. 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 ...

  9. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’

    Linux环境 Mysql+Hibernate command denied to user 错误 错误信息 如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

随机推荐

  1. 关于querySelectorAll的一个坑

    刚学JS的DOM操作时,就知道了匹配一堆元素,会获得NodeList和HTMLCollection这两个对象,不过当时并没有深入去研究两者的区别 因为无论是NodeList还是HTMLCollecti ...

  2. 从零自学Hadoop(23):Impala介绍及安装

    阅读目录 序 介绍 安装 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 上一篇, ...

  3. Ionic3新特性--页面懒加载1

    Ionic3新的懒加载机制给我带来了如下新特性: 避免在每一个使用到某Page的Module或其他Page中重复的import这个类(需要写一堆路径) 允许我们通过字符串key在任何想使用的地方获取某 ...

  4. Nodejs-express 4.0框架 简单介绍

    http://www.expressjs.com.cn/4x/api.html  这个是express API的中文手册 但是只是翻译了一点 英语比较差比较难受. 1. 关于安装 现在网上查的时候有一 ...

  5. 夜神模拟器与HBuilder连接/cmd运行提示符/执行夜神模拟器命令/执行HBuilder命令

    第一步:启动HBuilder和夜神模拟器 第二步:通过运行电脑命令CMD进入(电脑运行命令的快捷键是:windows键+R2.Ctrl键与Alt键之间的那个键就是windows键或者点击左下角开始图标 ...

  6. [转] .NET领域驱动设计—看DDD是如何运用设计模式颠覆传统架构

    阅读目录: 1.开篇介绍 2.简单了解缘由(本文的前期事宜) 3.DomainModel扩展性(运用设计模式设计模型变化点) 3.1.模型扩展性 3.2.设计模式的使用(苦心专研的设计模式.设计思想可 ...

  7. [0] Devexpress 控件参数集合

    gridview控件/统计功能 比如对“数量”列进行统计,只要在GridControl的设计器中设置SummaryItem:   SummaryItem.DisplayFormat = "{ ...

  8. ThinkPHP3.2 生成二维码

    下面是整合将phpqrcode整合到TP生成二维码就可以解决这个问题了.其实也很简单,使用方法如下:先下载附件解压至ThinkPHP/Extend/Vendor目录,目录不存在自己创建.      v ...

  9. AI相关 TensorFlow -卷积神经网络 踩坑日记之一

    上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...

  10. IE11中navigator.userAgent的变化

    在原来判断浏览器是否是ie时,我们可以根据navigator.userAgent中时候有MSIE,但是IE11进行变革,userAgent中不在包含MSIE字段, 在实际项目中,入到类似的在控制台报错 ...