繁体   English   中英

Spring boot data.sql 不会在 Postgresql 中初始化数据

[英]Spring boot data.sql does not initialize data in 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 来初始化它。

我错过了什么?

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 库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM