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

标签 postgresql hibernate spring-boot docker gradle

正在开发 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')
}

这是 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

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

INSERT INTO color_type (color) VALUES ('blue'),('green'),('purple'),('red');

下面是这个模型的一个例子:

package idealab.api.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name = "color_type")
public class ColorType {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany(targetEntity=PrintModel.class, mappedBy="colorTypeId")   
    private Set<PrintModel> printModel;

    @Column(name = "color", nullable = false)
    private String color;

    public ColorType(Integer id, String color) {

        this.color = color;
    }

    //getters and setters
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

感谢您提供的任何帮助!

最佳答案

import.sql是 Hibernate native 功能,只有在 hibernate.hbm2ddl.auto 时才会执行设置为创建创建-删除

但是现在你正在设置:

spring.jpa.generate-ddl=true
hibernate.hbm2ddl.auto=create 
  • spring.jpa.generate-ddl=true将设置 hibernate.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

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

关于postgresql - 为什么我的 import.sql 文件没有自动运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642901/

相关文章:

java - 删除未保存的对象不会引发异常

java - hbm2ddl.auto 设置为 "UPDATE",但它试图在每次插入之前创建表

Spring-boot如何在被调用方法中使用 Autowiring 的类属性

java - Spring Boot ClassNotfoundException EmbeddedServletContainerCustomizer

java - 设置 Zookeeper 仅为每个应用程序实例传递一次配置 [Spring]

用于查找 friend 签到的 SQL 查询

java - 运行子查询决定是否跳过外部查询,如果跳过不发送参数?

java - Hibernate/JPA JPQL 在查询 Map<String,String> 字段时出现错误的 SQL

postgresql - psql 在启动时挂起

postgresql - 输入以下命令时我没有看到任何输出 :select inet_server_addr();