繁体   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