Spring Boot Data.sql未在PostgreSQL中初始化数据 [英] Spring boot data.sql does not initialize data in Postgresql

查看:309
本文介绍了Spring Boot Data.sql未在PostgreSQL中初始化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Rest Api应用程序。
这是实体

I have a Spring Rest Api application. This is the entity:

@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 文件夹:

Here is my data.sql in the resources folder:

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来初始化它。

The table exists, I can add data other ways, but I would prefer the data.sql to initalize it.

我缺少什么?

推荐答案

更改 spring.jpa.properties .hibernate.ddl-auto = create spring.jp application.properties 文件中的a.hibernate.ddl-auto = create 并更改 GeneratedValue 示例实体中的注释,如下所示:

Change spring.jpa.properties.hibernate.ddl-auto=create to spring.jpa.hibernate.ddl-auto=create in the application.properties file And Change GeneratedValue annotation in the Example entity as below:

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

仅供参考:您也可以使用Liquibase或Flyway库。

FYI: You can use Liquibase or Flyway libraries too.

这篇关于Spring Boot Data.sql未在PostgreSQL中初始化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆