消息 -“无法读取 hi 值 - 您需要填充表:hibernate_sequence"

2022-01-22 00:00:00 sql postman java spring-boot Hibernate

我的问题如下:当我在邮递员"应用程序中创建 POST 请求时.这就是我尝试发布的内容

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

我收到以下错误:

<代码>{"时间戳": "2018-11-19T20:16:00.486+0000",状态":500,"error": "内部服务器错误","message": "无法读取 hi 值 - 您需要填充表:hibernate_sequence;嵌套异常是 org.hibernate.id.IdentifierGenerationException:无法读取 hi 值 - 您需要填充表:hibernate_sequence",路径":/api/ver01/product"}

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

这是我的产品类.

包 com.hubertkulas.webstore.store.archetype;导入 com.fasterxml.jackson.annotation.JsonFormat;导入 com.fasterxml.jackson.annotation.JsonIgnoreProperties;导入 javax.persistence.Entity;导入 javax.persistence.GeneratedValue;导入 javax.persistence.GenerationType;导入 javax.persistence.Id;导入 java.math.BigDecimal;导入java.sql.Date;@实体@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})公共类产品{@ID@GeneratedValue(策略 = GenerationType.AUTO)私人长ID;私人布尔联系人;私人字符串电子邮件;私有字符串类别;私有字符串名称;私人字符串城市;私有字符串模型;私人 BigDecimal 价格;@JsonFormat(形状=JsonFormat.Shape.STRING,模式=MM-dd-yyyy")私人日期日期;公共字符串 getName() {返回名称;}公共无效集合名称(字符串名称){this.name = 名称;}公共字符串 getEmail() {返回电子邮件;}公共无效 setEmail(字符串电子邮件){this.email = 电子邮件;}公共字符串 getCity() {返回城市;}公共无效setCity(字符串城市){this.city = 城市;}公共字符串 getCategory() {返回类别;}公共无效 setCategory(字符串类别){this.category = 类别;}公共字符串 getModel() {返回模型;}公共无效setModel(字符串模型){this.model = 模型;}公共 BigDecimal getPrice() {退货价格;}公共无效 setPrice(BigDecimal 价格){this.price = 价格;}公共日期 getDate() {归期;}公共无效 setDate(日期日期){this.date = 日期;}公共布尔isContact(){返回联系方式;}公共无效setContact(布尔联系人){this.contact = 联系人;}公共长 getId() {返回标识;}//id 的设置器,因为 Jackson 会使用它公共无效setId(长ID){这个.id = id;}}

这是我的 ProductController 类

com.hubertkulas.webstore.store.controllers 包;导入 com.hubertkulas.webstore.store.archetype.Product;导入 com.hubertkulas.webstore.store.jparepository.ProductRepository;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.http.HttpStatus;导入 org.springframework.web.bind.annotation.*;导入 java.util.List;@RestController@RequestMapping("api/ver01/product")公共类产品控制器 {//调用ProductController时注入ProductRepository@自动连线私有产品存储库产品存储库;@GetMapping公开列表<产品>列表() {//查找所有记录并返回返回 productRepository.findAll();}@PostMapping@ResponseStatus(HttpStatus.OK)公共无效创建(@RequestBody 产品产品){productRepository.save(产品);}@GetMapping("/{id}")public Product get(@PathVariable("id") long id){//返回带有添加 id 的特定记录返回 productRepository.getOne(id);}}

这是我的 ProductRepository 接口

包com.hubertkulas.webstore.store.jparepository;导入 com.hubertkulas.webstore.store.archetype.Product;导入 org.springframework.data.jpa.repository.JpaRepository;//使用Jpa进行CRUD操作公共接口 ProductRepository 扩展了 JpaRepository<Product, Long>{}

这是我的数据库

创建表产品(id BIGINT 非空,联系 BOOLEAN NOT NULL,电子邮件 VARCHAR,VARCHAR 类别,名称 VARCHAR,城市 VARCHAR,日期日期时间,价格数字,模型 VARCHAR,主键(id));创建表hibernate_sequence(next_val BIGINT);插入产品(id、联系人、电子邮件、类别、名称、城市、日期、价格)值(1、1、abraham@site.com"、电子"、Abraham Westbrom"、新约克', 4419619200000, '3250');插入产品(id、联系人、电子邮件、类别、名称、城市、日期、价格)值 (2, 1, 'udon@site.com', '电子', 'Udon Hon', 'London',4419619200000, '799');插入产品(id、联系人、电子邮件、类别、名称、城市、日期、价格)值 (3, 0, 'mateuszsinus@site.com', '软件', 'Mateusz Sinus','华沙', 4419619200000, '10000');INSERT INTO hibernate_sequence (next_val) VALUES (4);

解决方案

你的 hibernate_sequence 表错误.

参见 2.6.10.使用标识符表:

<块引用>

创建表hibernate_sequences(sequence_name VARCHAR NOT NULL,next_val 整数非空)

My problem is as follows: When i create POST request in "Postman" app. This is what i try to POST

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

I am getting the following error:

{
"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"
}

I was looking for answer in search box but none of them helped me. So i think that the problem is in sql code but I am not sure. Whole project is written in intelliJ IDE.

This is my Product class.

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;
}
}

This is my ProductController class

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);
}

}

This is my ProductRepository Interface

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> {
}

And this is my database

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);

解决方案

Your hibernate_sequence table is wrong.

See 2.6.10. Using identifier table:

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

相关文章