自动创建表时,多了一张表hibernate_sequence,为什么?
自动创建表时,多了一张表hibernate_sequence,为什么?
自动创建表时,多了一张表hibernate_sequence,为什么?
2018-06-22
从网上复制下来的
------------------------这是一条分割线---------------------------
环境:@GeneratedValue(strategy = GenerationType.AUTO),数据库用的mysql
问题:
1.发现数据库保存的时候会生成hibernate_sequence表,用来记录其他表的主键。若删除该表,将报错could not read a hi value - you need to populate the table: hibernate_sequence。
2.服务器重启时主键从1开始记录,由于数据库里有主键为1的数据,于是会报主键重复的错误。
解决方法:
将@GeneratedValue(strategy = GenerationType.AUTO)改为@GeneratedValue(strategy = GenerationType.IDENTITY) 。
原因:
@GeneratedValue(strategy = GenerationType.AUTO)主键增长方式由数据库自动选择,当数据库选择sequence方式时,出现如上错误。
@GeneratedValue(strategy = GenerationType.IDENTITY) 要求数据库选择自增方式,oracle不支持此种方式。
@GeneratedValue(strategy = GenerationType.SEQUENCE)采用数据库提供的sequence机制生成主键。mysql不支持此种方式。
@GeneratedValue(strategy = GenerationType.TABLE)没太看懂怎么存储的。
-------------------------以上就是我觉得大概能解决的方案了-------------------
//Girl类
package com.han.girl;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Girl {
private int id;
private String cupSize;
private int age;
public Girl() {
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password: 1234
jpa:
hibernate:
ddl-auto: create
show-sql: true这是我的代码
举报