最近几天几天做项目用到了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. 实现图标Icon+文字在div里自动中心居中(水平垂直居中)

    已知div行高设置text-align:center文字会自动居中. 通过:before来设置icon的地址和高宽. 需要设置图片默认的垂直居中条件,与文字一致,为text-bottom. 设置图片行 ...

  2. Libevent源码分析—event_set()

    初始化完event_base后,下面要初始化event,通过调用event_set()实现 .相关源码位于event.c event_set() void event_set(struct event ...

  3. Git基础-打标签

    打标签 同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做.本节我们一起来学习如何列出所有可用的标签,如何新建标签,以 ...

  4. JavaSE教程-01初识Java

    1.计算机的概念 软件+硬件 2.操作系统 Windows.Mac.Linux.Unix等 3.计算机编程语言 计算机语言是一种人与计算机沟通的媒介. 分类: 机器语言:都是基于二进制的方式,由0和1 ...

  5. java之内部类

    最近学了java,对内部类有一点拙见,现在分享一下 所谓内部类(nested classes),即:面向对象程序设计中,可以在一个类的内部定义另一个类. 内部类不是很好理解,但说白了其实也就是一个类中 ...

  6. 基于C#的接口自动化测试(一)

    其实就是找个地方然后给关键的代码做个笔记什么的-- 字符串访问API接口,访问方法为POST: string url = URL; string RequestParam = Param; strin ...

  7. Bash中单引号和双引号的区别

    单引号和双引号的区别 单引号:必须成对使用,它可以保护所有的字符不被翻译.如变量$1,和奇数个单引号的作用相同,偶数个单引号=1个双引号双引号:必须成对出现,它可以保护一些元字符不被翻译,但允许变量和 ...

  8. 石头剪刀布 R语言统计分析

    关于石头剪刀布,做出了详细的分析,具体ppt见地址 http://files.cnblogs.com/files/GMGHZ971322/R%E8%AF%AD%E8%A8%80.pptx 16级电商三 ...

  9. 最全Oracle环境搭建之.NET程序员初遇Oracle

    前言:如果你习惯了傻瓜式的一步步安装,那么Oracle和.NET搭配,绝对会让你头痛不已. 目前我不敢保证自己理解的Oracle理论部分100%正确,但环境安装过程一定可以收藏以备不时之需. 路这么长 ...

  10. nginx二级域名配置自动跳转到一级域名

    nginx二级域名配置自动跳转到一级域名 rewrite配置内容: if ($http_host !~ "^www.aaa.com$") { rewrite ^(.*) http: ...