Home > Back-end >  Spring boot is not saving data of data.sql file into h2 database on initialization
Spring boot is not saving data of data.sql file into h2 database on initialization

Time:10-11

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();
    }

}

schema.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)
);

data.sql

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

application.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 of data.sql file is not getting picked in H2-Database. H2-Database is working fine for the rest other cases. I am using JPA for other DB-related operations. If you want some other files then please tell me so that I can upload it. Please help me to know way this is not working.

CodePudding user response:

I don't know exactly all context. Why don't you set value of ddl-auto config in application.yml. Like below. In my case, it's working.

Note that If you set ddl-auto is validate, you have to match between columns of schema.sql and fields of entity.

spirng: 
  jpa:
    hibernate:
      ddl-auto: validate

Spring documents says that 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 here.

CodePudding user response:

After adding this line to my application.properties file the code worked.

spring.jpa.hibernate.ddl-auto= update
  • Related