I have added query inside data.sql file. But it’s not getting entered in table while triggering api.
In my appliction.properties:
spring.application.name=virtualbookstore
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.mvc.format.date=yyyy-MM-dd
spring.jpa.defer-datasource-initialization=true
logging.level.org.springframework.web=debug
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3307/books
spring.datasource.username=books-user
spring.datasource.password=dummybooks
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.open-in-view=false
DATA.SQL:
insert into book(ID, BOOKNAME, AUTHORNAME, PUBLISHEDDATE)
values (101, 'The Psychology of Money', 'Mark Manson', CURRENT_DATE());
Controller Method:
@GetMapping(path = "book-list")
public String getAllBooks(ModelMap model) {
List<Book> booklist = bookRepo.findAll();
System.out.println(booklist.toString());
model.addAttribute("booklist", booklist);
return "bookList";
}
Issue
When I trigger the API to fetch the list of books, the inserted book is not showing up. The data.sql file doesn’t seem to be executed on application startup.
What I’ve Tried
Verified that the table schema exists and is correct.
Ensured that the data.sql file is in the src/main/resources directory.
Expected Result
The book inserted via data.sql should be present in the database when the application starts, and the /book-list API should return the inserted book.