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

Spring Boot 3(Jakarta版本)下如何用Hibernate生成DDL脚本?

Spring Boot 3(Hibernate 6)生成DDL创建脚本的配置问题

问题描述

在Spring Boot 2中,添加以下配置就能生成DDL创建脚本:

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
spring.jpa.properties.hibernate.hbm2ddl.delimiter=;

升级到Spring Boot 3后,我把配置里的javax替换成了jakarta:

spring.jpa.properties.jakarta.persistence.schema-generation.create-source=metadata
spring.jpa.properties.jakarta.persistence.schema-generation.scripts.action=create
spring.jpa.properties.jakarta.persistence.schema-generation.scripts.create-target=create.sql
spring.jpa.properties.hibernate.hbm2ddl.delimiter=;

但create.sql并没有生成。想问下是不是配置属性已经完全变更?在Hibernate 6里该怎么实现这个功能?

解决方案

Hibernate 6对Schema生成的配置做了调整,你可以通过两种方式解决:

方式一:使用Hibernate原生配置

直接替换为以下Hibernate专属配置,就能正常生成create.sql:

spring.jpa.properties.hibernate.schema-generation.create_source=metadata
spring.jpa.properties.hibernate.schema-generation.scripts.action=create
spring.jpa.properties.hibernate.schema-generation.scripts.create_target=create.sql
spring.jpa.properties.hibernate.hbm2ddl.delimiter=;

方式二:修正JPA标准配置

如果想继续用JPA标准属性,需要配合开启DDL生成开关,完整配置如下:

spring.jpa.generate-ddl=true
spring.jpa.properties.jakarta.persistence.schema-generation.create-source=metadata
spring.jpa.properties.jakarta.persistence.schema-generation.scripts.action=create
spring.jpa.properties.jakarta.persistence.schema-generation.scripts.create-target=create.sql
spring.jpa.properties.hibernate.hbm2ddl.delimiter=;

额外注意:如果是Web应用,启动时会自动初始化JPA上下文触发生成逻辑;如果是非Web应用(比如控制台程序),要确保EntityManagerFactory被正确创建,否则Schema生成不会执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:23:14