java - Spring boot data.sql 不会在 Postgresql 中初始化数据
问题描述
我有一个 Spring Rest Api 应用程序。这是实体:
@Entity
@Table(name="a_example")
public class Example
{
@Id
@GeneratedValue
private Long id;
private String name;
private String description;
@Column(unique = true, nullable = false)
private String filename;
}
这是资源文件夹中的data.sql :
INSERT INTO a_example(name, description, filename) VALUES('Xyz', 'Lorem ipsum', 'xyz');
INSERT INTO a_example(name, description, filename) VALUES('Pqr', 'Lorem ipsum', 'pqr');
还有我的application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/xyz
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.data=classpath:data.sql
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
该表存在,我可以通过其他方式添加数据,但我更喜欢 data.sql 来初始化它。
我错过了什么?
解决方案
在文件中更改 spring.jpa.properties.hibernate.ddl-auto=create
为并在实体中更改注释,如下所示:spring.jpa.hibernate.ddl-auto=create
application.properties
GeneratedValue
Example
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
仅供参考:您也可以使用 Liquibase 或 Flyway 库。
推荐阅读
- maven - 为什么我需要添加 Spring Boot 插件,但我不需要在 Maven pom.xml 中添加安装插件?
- design-patterns - Flux 架构模式 VS 中介者设计模式?
- reactjs - 如何在搜索输入期间在屏幕之间动态切换并更新 url
- api - 在 nuxtjs 中使用 axios 向 api 发送 http 请求 shwoing net::ERR_HTTP2_PROTOCOL_ERROR
- php - 使用 Apache 设置 Symfony
- c# - 查找字符数组中的第一个不同字符及其索引
- vuex-orm - 使用“vuex-orm-decorators”时出现“TypeError:this.types 不是函数”
- ios - iOS中的Ionic 4 Http请求失败
- c# - 具有不变名称“System.Data.SqlClient”的 ADO.NET 提供程序未注册
- facebook-graph-api - 如何获取授予事件端点权限的 Facebook 访问令牌?