【问题标题】:Spring boot data.sql does not initialize data in PostgresqlSpring boot data.sql 不会在 Postgresql 中初始化数据
【发布时间】:2020-07-08 05:25:10
【问题描述】:

我有一个 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。尝试添加此属性以及spring.jpa.show-sql=true 以查看是否触发了 sql

标签: java spring postgresql hibernate spring-boot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-29
    • 2017-12-18
    • 2021-09-29
    • 2020-04-07
    • 2018-02-14
    • 2021-11-30
    • 1970-01-01
    • 2021-02-16
    • 2018-03-22
    相关资源
    最近更新 更多