简体   繁体   English

Spring JPA Hibernate 无法自动生成 Id H2 表

[英]Spring JPA Hibernate Fails To Auto-generate Id H2 table

I'm getting an exception when trying to create a table with auto-generated Id:尝试使用自动生成的 Id 创建表时出现异常:

org.hibernate.tool.schema.spi.CommandAcceptanceException: "Error executing DDL "create table seat (id bigint not null, description varchar(255), num integer not null, price decimal(19,2), row char(255) not null, primary key (id))" via JDBC Statement". org.hibernate.tool.schema.spi.CommandAcceptanceException: "Error executing DDL "create table seat (id bigint not null, description varchar(255), num integer not null, price decimal(19,2), row char(255)不是 null,主键(id))“通过 JDBC 语句”。

It looks like the generated SQL doesn't recognize '@GeneratedValue(strategy = GenerationType.TABLE)' annotation.看起来生成的 SQL 无法识别“@GeneratedValue(strategy = GenerationType.TABLE)”注释。 Looks like it's a very common problem with either Hibernate or adaptor.看起来这是 Hibernate 或适配器的一个非常常见的问题。

Now, before you discard this question as a duplicate, I've gone through all q/a with a similar problem and tried all solutions suggested there.现在,在您将此问题作为重复问题丢弃之前,我已经完成了所有有类似问题的 q/a 并尝试了那里建议的所有解决方案。 I've also tried to generate Id keys myself and tried to set spring.jpa.properties.hibernate.hbm2ddl.auto to 'delete-create'我也尝试自己生成 Id 密钥,并尝试将 spring.jpa.properties.hibernate.hbm2ddl.auto 设置为'delete.auto

application.properties应用程序属性

spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa
spring.datasource.password =
spring.jpa.properties.hibernate.hbm2ddl.auto=update

Entity Class实体 Class

import java.math.BigDecimal
import javax.persistence.*

@Entity
data class Seat(
@Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id")
                val id: Long,
                val row: Char,
                val num: Int,
                val price: BigDecimal,
                val description: String) {
    override fun toString(): String = "Seat $row-$num $$price ($description)"
}

Service Constructor服务构造器

constructor() {
        ...
        for (row in 1..15) {
            for (num in 1..36) {
                hiddenSeats.add(Seat(0, (row+64).toChar(), num, getPrice(row,num), getDescription(row,num) ))
            }
        }
    }

Things I tried: - strategy=GenerationType.AUTO change to.SEQUENCE and.TABLE - add this to application.properties:我尝试过的事情: - strategy=GenerationType.AUTO 更改为.SEQUENCE 和.TABLE - 将其添加到 application.properties:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect

I tried to generate the Id myself but to no success.我试图自己生成 ID,但没有成功。 I must be missing something as I'm new Kotlin, Spring Boot and Hibernate, but hit the wall here.我必须缺少一些东西,因为我是新的 Kotlin、Spring 引导和 Hibernate,但在这里碰壁了。 Please advise!请指教!

jdk-14, Spring Boot v2.2.6.RELEASE, Using dialect: org.hibernate.dialect.H2Dialect jdk-14,Spring 启动 v2.2.6.RELEASE,使用方言:org.hibernate.dialect.H2Dialect

Here is the problem val row: Char , ROW is a reserved word in SQL.这是问题val row: Char , ROW 是 SQL 中的保留字。 change that var更改该变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM