I am creating a Spring Boot application using the H2 database. I am constantly getting the following error:
Table "THINGS_TO_DO" not found; SQL statement:
insert into things_to_do (id, name, verified) values (1, 'TestUser1', 1) [42102-197]
And, I feel this is logical since I don't know where to pass this table name in the application. Also, what should the table name be - is there some specific name that the table must have?
My ThingsToDo.java is like below:
package me.hiboy.springboot.microservice.example.todo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="things_to_do")
public class ThingsToDo {
@Id
private Long id;
@Column(name="name")
private String name;
@Column(name="verified")
private int verificationStatus;
private String task;
public ThingsToDo() {
}
public ThingsToDo(Long id, String name, int verificationStatus, String task) {
super();
this.id=id;
this.name=name;
this.verificationStatus=verificationStatus;
this.task=task;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getVerificationStatus() {
return verificationStatus;
}
public String getTask() {
return task;
}
}
The controller ThingsToDoController.java is as follows:
package me.hiboy.springboot.microservice.example.todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ThingsToDoController {
@Autowired
ThingsToDoRepository repository;
@GetMapping("/")
public String index() {
return "Hello from the ToDo Controller\n";
}
@GetMapping("/todo/{name}")
public ThingsToDo getThingsToDo(@PathVariable String name) {
ThingsToDo thingToDo=repository.findByName(name);
return thingToDo;
}
}
Repository ThingsToDoRepository is:
package me.hiboy.springboot.microservice.example.todo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ThingsToDoRepository extends JpaRepository<ThingsToDo, Long> {
ThingsToDo findByName(String name);
}
Application.properties is:
spring.application.name=todo-service
server.port=8080
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.initialize=true
data.sql is:
insert into things_to_do (id, name, verified) values (1, 'TestUser1', 1);
insert into things_to_do (id, name, verified) values (2, 'TestUser2', 0);
I don't think pom.xml
is required - in case it is, kindly lemme know and I will post that as well. Thanks.
Edit: The one with the main() method is here:
package me.hiboy.springboot.microservice.example.todo.springbootmicroservicetodoservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMicroserviceTodoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMicroserviceTodoServiceApplication.class, args);
}
}
Edit: All the answers given so far do not help at all.