最近几天几天做项目用到了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. 分布式版本控制git常见问题之gitignore冲突

    所见即所得,建议大家用可视化的SourceTree专为git打造的,非常好用,废话不多说了. 原因是有人提交了.gitignore里面的内容,所以和本地的不一样,这样就有问题,那么pull都不可以,所 ...

  2. Handling Class Imbalance with R and Caret - An Introduction

    When faced with classification tasks in the real world, it can be challenging to deal with an outcom ...

  3. Javascript加载执行顺序

    本文主要内容 一.不同位置的script标签执行顺序 二.document.ready和window.onload的区别 一.不同位置的script标签执行顺序 整个加载的过程从解析头部开始,比如ht ...

  4. cpp(第十章)

    1. const class & func(const class &) const { do something.. } 第一个const返回后的类不允许被赋值,第二个const不允 ...

  5. 获取Skype用户IP地址

    #!/usr/bin/env bash ESC_SEQ="\x1b[" COL_RESET=$ESC_SEQ"39;49;00m" COL_RED=$ESC_S ...

  6. iOS bug调试技巧学习----breakpoint&condition

    给断点添加条件 - (void)testCondition2 { NSArray *array = @[@"我们", @"一起", @"来" ...

  7. windows手动搭建Kotlin命令行环境

    kotlin官网:https://kotlinlang.org 我们可以从GitHub下载.目前最新版本是1.1.2-5 https://github.com/JetBrains/kotlin/rel ...

  8. DNS域名解析的过程

    一.主机解析域名的顺序 1.找缓存 2.找本机的hosts文件 3.找DNS服务器 注意: 配置IP和主机名时,要记得修改/etc/hosts文件,因为有些应用程序在主机内的进程之间通信的时候,会本机 ...

  9. [转]TOMCAT配置多端口

    一.Tomcat 安装后本身提供了一个server,端口配置默认是8080,对应目录为:..\Tomcat 6.0\webapps二.Tomcat 6.0 配置多个端口,其实也就是给Tomcat增加几 ...

  10. [原创]Floodlight+ovs的基本使用

    一.配置好openflow交换机 配置好交换机的管理地址,可先用串口登,使管理口地址与controller地址在同一个网络中. 在交换机上配置controller地址: 如: 先用命令新建一个brid ...