【问题标题】:Spring JPA Hibernate Fails To Auto-generate Id H2 tableSpring JPA Hibernate 无法自动生成 Id H2 表
【发布时间】:2020-07-27 19:46:16
【问题描述】:

尝试使用自动生成的 ID 创建表时遇到异常:

org.hibernate.tool.schema.spi.CommandAcceptanceException:“执行 DDL 时出错”创建表位(id bigint 不为 null,描述 varchar(255),num integer 不为 null,price decimal(19,2),row char (255) 不为空,主键 (id))”通过 JDBC 语句”。

生成的 SQL 似乎无法识别“@GeneratedValue(strategy = GenerationType.TABLE)”注释。看起来这是 Hibernate 或适配器的一个非常常见的问题。

现在,在您将此问题作为重复问题丢弃之前,我已经完成了所有有类似问题的 q/a 并尝试了那里建议的所有解决方案。我也尝试自己生成 Id 密钥并尝试将 spring.jpa.properties.hibernate.hbm2ddl.auto 设置为“删除创建”

application.properties

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

实体类

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

服务构造器

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

我尝试过的事情: - strategy=GenerationType.AUTO 更改为 .SEQUENCE 和 .TABLE - 将此添加到 application.properties:

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

我尝试自己生成 ID,但没有成功。我一定是缺少一些东西,因为我是新的 Kotlin、Spring Boot 和 Hibernate,但在这里碰壁了。请指教!

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

【问题讨论】:

    标签: java hibernate spring-boot jpa h2


    【解决方案1】:

    这里是问题val row: Char,ROW是SQL中的保留字。改变那个变量

    【讨论】:

    • 这不是 SQL 中的保留字,但显然在 H2 中,因为我能够使其与 mySql 一起正常工作。无论如何,我会将其标记为答案,因为您足够接近。现在很好奇,该死的演示者是如何让它在教程中工作的......无论如何,谢谢!
    猜你喜欢
    • 2023-02-17
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2016-10-01
    • 2022-08-18
    • 2019-06-12
    相关资源
    最近更新 更多