簡體   English   中英

Spring 引導未在初始化時將 data.sql 文件的數據保存到 h2 數據庫中

[英]Spring boot is not saving data of data.sql file into h2 database on initialization

EmployeeModelRead.java

package com.batchjob.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPLOYEE_MODEL_READ")
public class EmployeeModelRead {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;

    @Column(name = "NAME")
    private String name;
    @Column(name = "DOJ")
    private String doj;
    @Column(name = "AGE")
    private int age;
    @Column(name = "ROLE")
    private String role;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDoj() {
        return doj;
    }

    public void setDoj(String doj) {
        this.doj = doj;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public EmployeeModelRead(long id, String name, String doj, int age, String role) {
        super();
        this.id = id;
        this.name = name;
        this.doj = doj;
        this.age = age;
        this.role = role;
    }

    public EmployeeModelRead() {
        super();
    }

}

架構.sql

drop table if exists employee_model_read;

create table EMPLOYEE_MODEL_READ(
  ID BIGINT AUTO_INCREMENT PRIMARY KEY,
  AGE INTEGER(10),
  DOJ VARCHAR(255),
  NAME VARCHAR(255),
  ROLE VARCHAR(255)
);

數據.sql

insert into employee_model_read(AGE,DOJ,NAME,ROLE) values (23,'25-09-2020','RAM','Front End Developer');

應用程序.properties

spring.datasource.url=jdbc:h2:mem:batchjob
spring.h2.console.enabled=true
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.batch.job.enabled=false

data.sql 文件的數據未在 H2 數據庫中選取。 H2 數據庫在 rest 其他情況下工作正常。 我正在使用 JPA 進行其他與數據庫相關的操作。 如果您想要其他文件,請告訴我,以便我上傳。 請幫助我知道這是行不通的方式。

將此行添加到我的application.properties文件后,代碼就可以工作了。

spring.jpa.hibernate.ddl-auto= update

我不完全了解所有上下文。 為什么不在 application.yml 中設置ddl-auto config 的值。 如下圖。 就我而言,它正在工作。

請注意,如果將ddl-auto設置為驗證,則必須在 schema.sql 的列和實體的字段之間進行匹配。

spirng: 
  jpa:
    hibernate:
      ddl-auto: validate

Spring 文檔說In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql. In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql. 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM