无法读取高值-您需要填充表:hibernate_sequence。

13

我的问题如下:当我在 "Postman" 应用程序中创建 POST 请求时,我尝试 POST 的内容如下:

  {"name": "John Doe", "email":"jdoe@test.com", "city": "London"}

我遇到了以下错误:

{
"timestamp": "2018-11-19T20:16:00.486+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not read a hi value - you need to populate the table: hibernate_sequence; nested exception is org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence",
"path": "/api/ver01/product"
}

我在搜索框中寻找答案,但没有一个能够帮助我。所以我认为问题在SQL代码中,但我不确定。整个项目都是在IntelliJ IDE中编写的。

这是我的Product类。

package com.hubertkulas.webstore.store.archetype;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.math.BigDecimal;
import java.sql.Date;

@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private boolean contact;
private String email;
private String category;
private String name;
private String city;

private String model;
private BigDecimal price;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy")
private Date date;



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal price) {
    this.price = price;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public boolean isContact() {
    return contact;
}

public void setContact(boolean contact) {
    this.contact = contact;
}

public Long getId() {
    return id;
}

// setter for id because Jackson will use it
public void setId(Long id) {
    this.id = id;
}
}

这是我的ProductController类

package com.hubertkulas.webstore.store.controllers;
import com.hubertkulas.webstore.store.archetype.Product;
import com.hubertkulas.webstore.store.jparepository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/ver01/product")
public class ProductController {

//injecting ProductRepository when ProductController is called
@Autowired
private ProductRepository productRepository;

@GetMapping
public List<Product> list() {
    //finds all of the records and returns it
   return productRepository.findAll();
}

@PostMapping
@ResponseStatus(HttpStatus.OK)
public void create(@RequestBody Product product){
    productRepository.save(product);
}



@GetMapping("/{id}")
public Product get(@PathVariable("id") long id){
    // return specific record with added id
    return productRepository.getOne(id);
}

}

这是我的ProductRepository接口

package com.hubertkulas.webstore.store.jparepository;

import com.hubertkulas.webstore.store.archetype.Product;
import org.springframework.data.jpa.repository.JpaRepository;

//Using Jpa for CRUD operations
public interface ProductRepository extends JpaRepository<Product, Long> {
}

这是我的数据库

CREATE TABLE
product
(
    id BIGINT NOT NULL,
    contact BOOLEAN NOT NULL,
    email VARCHAR,
    category VARCHAR,
    name VARCHAR,
    city VARCHAR,
    date DATETIME,
    price NUMERIC,
    model VARCHAR,
    PRIMARY KEY (id)
);

CREATE TABLE
hibernate_sequence
(
    next_val BIGINT
);

INSERT INTO product (id, contact, email, category, name, city, date, price)
VALUES (1, 1, 'abraham@site.com', 'Electronics', 'Abraham Westbrom', 'New 
York', 4419619200000, '3250');
INSERT INTO product (id, contact, email, category, name, city, date, price)
VALUES (2, 1, 'udon@site.com', 'Electronics', 'Udon Hon', 'London', 
4419619200000, '799');
INSERT INTO product (id, contact, email, category, name, city, date, price)
VALUES (3, 0, 'mateuszsinus@site.com', 'Software', 'Mateusz Sinus', 
'Warsaw', 4419619200000, '10000');

INSERT INTO hibernate_sequence (next_val) VALUES (4);

请查看此帖子:https://confluence.atlassian.com/confkb/getting-generate-could-not-read-a-hi-value-java-sql-sqlexception-invalid-object-name-hibernate_unique_key-sqlexception-error-192873336.html 或许可以帮到您。 - argoth
4个回答

13

如果您正在使用Spring Boot为本地数据库创建模式,并配置JPA进行自动创建和删除,理想情况下,您不应该遇到此情况。

spring.jpa.hibernate.ddl-auto=create-drop

但在部署/生产环境中,您需要单独处理模式定义(DDL),因此 hibernate_sequence 需要有一个初始值,而 0 应该足够作为起始值。它告诉程序库从哪个数字开始自动生成 ID。

spring.jpa.hibernate.ddl-auto=validate

INSERT INTO <schema_name>.hibernate_sequence (next_val) VALUES (0);

上述方法适用于MYSQL。


仅使用INSERT命令才能解决我的问题。谢谢! - Dinesh Shekhawat

2
您可以添加

标签


spring:
jpa:hibernate:ddl-auto: create-drop 

在您的application.yml文件或application.properties文件中,只有在截断表时才执行此操作,否则您可以添加INSERT INTO <schema_name>.hibernate_sequence (next_val) VALUES (1);

1

你的 hibernate_sequence 表有误。

请参考 2.6.10. 使用标识符表

create table hibernate_sequences(
    sequence_name VARCHAR NOT NULL,
    next_val INTEGER NOT NULL
)

我已经在我的“next_val BIGINT”中添加了“NOT NULL”。非常感谢,它解决了这个问题。 - HZK

1

删除表并使用next_val作为主键重新创建


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接