为什么我的 import.sql 文件没有自动运行?

moo*_*t94 4 postgresql hibernate gradle docker spring-boot

正在开发 java spring boot 应用程序。我们的 postgres 数据库是容器化的。我可以让 hibernate 自动创建表,但无法让它自动运行 import.sql 文件。你能帮我弄清楚发生了什么事吗?

这是 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE')
    }
}


apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

mainClassName = 'idealab.IdeaLabMain'

bootJar {
    baseName = 'idealab'
    excludeDevtools = false //TODO(e-carlin): In production this should be removed
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile('org.springframework.boot:spring-boot-devtools') // TODO(e-carlin): Make sure this isn't pulled in in the production jar
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.postgresql:postgresql')
    testCompile('junit:junit')
}
Run Code Online (Sandbox Code Playgroud)

这是 application.properties 文件:

logging.level.org.hibernate=DEBUG
debug=true

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=docker
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
hibernate.hbm2ddl.auto=create # TODO(e-carlin): This will wipe away the
                                     # database data. Good for dev not for prod
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

Run Code Online (Sandbox Code Playgroud)

这是位于 src/main/resources 中的 import.sql 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE')
    }
}


apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

mainClassName = 'idealab.IdeaLabMain'

bootJar {
    baseName = 'idealab'
    excludeDevtools = false //TODO(e-carlin): In production this should be removed
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile('org.springframework.boot:spring-boot-devtools') // TODO(e-carlin): Make sure this isn't pulled in in the production jar
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.postgresql:postgresql')
    testCompile('junit:junit')
}
Run Code Online (Sandbox Code Playgroud)

这是该模型的一个示例:

logging.level.org.hibernate=DEBUG
debug=true

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=docker
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
hibernate.hbm2ddl.auto=create # TODO(e-carlin): This will wipe away the
                                     # database data. Good for dev not for prod
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

Run Code Online (Sandbox Code Playgroud)

感谢您的任何帮助,您可以提供!

Ken*_*han 5

import.sqlhibernate.hbm2ddl.auto是 Hibernate 本机功能,只有设置为createcreate-drop时才会执行。

但现在你正在设置:

spring.jpa.generate-ddl=true
hibernate.hbm2ddl.auto=create 
Run Code Online (Sandbox Code Playgroud)
  • spring.jpa.generate-ddl=truehibernate.hbm2ddl.auto=update在幕后布景。
  • hibernate.hbm2ddl.auto=create无效并且没有任何效果,因为所有有效的 springboot JPA 属性都应以spring.jpa

所以最后,将hibernate.hbm2ddl.auto被设置为更新,因此import.sql不会被执行。

您可以通过更改为简单地修复它hibernate.hbm2ddl.auto=create

spring.jpa.properties.hibernate.hbm2ddl.auto=create
Run Code Online (Sandbox Code Playgroud)

注意:spring.jpa.generate-ddl将被覆盖spring.jpa.properties.hibernate.hbm2ddl.auto,因此您可以简单地将其删除。