I'm following this very simple tutorial to setup a REST API using Kotlin, Gradle and Spring Boot:
https://kotlinlang.org/docs/jvm-spring-boot-restful.html
But when I run the app (by clicking play) and visit localhost:8080 I get an error saying that the table "MESSAGES" is not found and the database is empty.
I know for sure that my application.properties is read because changing server.port
worked,
I've tried many things like moving schema.sql directly under main/resources, adding spring.jpa.hibernate.ddl-auto=none
in application.properties or marking the resources folder as "Source" but to no avail.
It seems my schema.sql is not being executed and I can't figure out why !
Versions: Windows 10, IntelliJ IDEA 2022.2.2, Java 17, Spring 2.7.4
application.properties:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./data/testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.schema=classpath:sql/schema.sql
spring.datasource.initialization-mode=always
sql/schema.sql:
CREATE TABLE IF NOT EXISTS messages (
id VARCHAR(60) DEFAULT RANDOM_UUID() PRIMARY KEY,
text VARCHAR NOT NULL
);
CodePudding user response:
Try adding these in your application.properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
CodePudding user response:
Solved it !
This IntelliJ tutorial was written for Spring 2.6 (no longer available in Spring Initializr) and in 2.7 these 2 keys changed in application.properties:
spring.datasource.initialization-mode
-> spring.sql.init.mode
spring.datasource.schema
-> spring.sql.init.schema-locations
full list of key changes
Replacing them in my application.properties worked !
New application.properties:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./data/testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.sql.init.schema-locations=classpath:sql/schema.sql
spring.sql.init.mode=always