Spring Boot的data.sql在PostgreSQL中不会初始化数据

3

我有一个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来初始化它。
我错过了什么吗?

2
我猜spring.jpa.properties.hibernate.ddl-auto=create应该是spring.jpa.hibernate.ddl-auto=create。尝试添加这个属性spring.jpa.show-sql=true,看看是否触发了SQL。 - pvpkiran
对我有效的唯一方法是这个属性 spring.datasource.initialization-mode=always,顺便说一下,它已经被弃用了,哈哈。 - Bigyan Devkota
1个回答

2

application.properties文件中,将spring.jpa.properties.hibernate.ddl-auto=create更改为spring.jpa.hibernate.ddl-auto=create。并且在Example实体中,将GeneratedValue注释更改如下:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

提供信息:您也可以使用Liquibase或Flyway库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接