[英]Spring boot is not saving data of data.sql file into h2 database on initialization
EmployeeModelRead.java 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架构.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数据.sql
insert into employee_model_read(AGE,DOJ,NAME,ROLE) values (23,'25-09-2020','RAM','Front End Developer');
application.properties应用程序.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. data.sql 文件的数据未在 H2 数据库中选取。 H2-Database is working fine for the rest other cases.
H2 数据库在 rest 其他情况下工作正常。 I am using JPA for other DB-related operations.
我正在使用 JPA 进行其他与数据库相关的操作。 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.
请帮助我知道这是行不通的方式。
After adding this line to my application.properties file the code worked.将此行添加到我的application.properties文件后,代码就可以工作了。
spring.jpa.hibernate.ddl-auto= update
I don't know exactly all context.我不完全了解所有上下文。 Why don't you set value of
ddl-auto
config in application.yml.为什么不在 application.yml 中设置
ddl-auto
config 的值。 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.请注意,如果将
ddl-auto
设置为验证,则必须在 schema.sql 的列和实体的字段之间进行匹配。
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.
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.
in here .在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.