You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Data JPA对接MySQL提示receipe表不存在无法自动建表问题

问题解决方案

导致表创建失败的核心原因是MySQL不支持你配置的SEQUENCE主键生成策略,搭配以下调整即可解决:

1. 修正Receipe实体主键生成策略

MySQL没有序列机制,将主键的生成策略替换为适配MySQL的自增策略:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val receipeId:Long=-1,

无需保留SEQUENCE相关的@SequenceGenerator注解。

2. 完善application.yml配置

追加数据库连接参数和JPA适配配置,避免编码、时区、命名策略导致的建表失败:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/hospital-management?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password:
  jpa:
    # 如果你用的是MySQL8,替换为MySQL8Dialect
    database-platform: org.hibernate.dialect.MySQL5Dialect
    show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    properties:
      hibernate:
        jdbc:
          lob:
            non_contextual_creation: true

3. 校验关联实体配置

确保Disease实体已经添加@Entity注解,且实体中存在名为problemId的主键字段,避免外键创建失败连带导致receipe表创建失败。如果不需要数据库层面的外键约束,可以调整关联注解关闭外键生成:

@ManyToOne(cascade = [CascadeType.ALL],fetch = FetchType.LAZY)
@JoinColumn(name = "problemId",referencedColumnName = "problemId", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
val disease: Disease ?=null,

4. 清理残留数据重启

到phpMyAdmin中清理hospital-management库中之前创建失败的残留表、约束,再重启项目即可自动生成receipe表。


内容的提问来源于stack exchange,提问作者Mama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 08:48:02