正在开发 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/