Spring boot classpath: The specified resource does not exist - java

I am using h2 as a database for testing. I have added sql scripts to /src/main/resources but spring boot always fails with "The specified resource does not exist." error.
Following are the configurations
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.initialize=true
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
ErrorTrace: https://paste.ee/p/1kHwd
I have referred many posts on this platform and tried changing the path to classpath:/data.sql but this did not work as well. I am facing the same issue with the flyway. Somehow Spring can not found these scripts in resources.
Am I missing something here? Any help is appreciated.

try to add
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
in your pom file

Related

Kotlin Spring Boot: database is empty despite creating a table in schema.sql

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
Main file gist
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
);
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
Try adding these in your application.properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Cant create h2 table via docker (sprint boot app)

Im bulding a rest api using spring boot.
I tried to create table in h2 db in various ways like using data.sql or schema.sql, but none of them actually create the tables so I wrote this code:
This code works perfectly when I run it via ide(intelij).
But when I try to run it using java "-jar target/projectName.jar" it doesn't create the table.
In addition, when I pack and run the app from docker the table doesn't being created.
Is someone know what I'm doing wrong?
application. properties:
Dockerfile:
Please refer this to see how to make db initialization work.
You can create a sql file as src/main/resources/schema-h2.sql and have application properties like
server.port=8090
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
spring.h2.console.enabled=true
spring.jpa.open-in-view=false
spring.jpa.show-sql=true
spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always
Property
spring.datasource.initialization-mode=always
will help you initialize database with the content written in schema-h2.sql file
Please make sure that spring.jpa.hibernate.ddl-auto=none is always set to none to run db initialization script properly.

Liquibase Integration Tests Using Main Database Instead of H2

I am trying to run integration tests in Java and I have liquibase integrated. Integration tests are running absolutely fine but the tests are referring to MySQL which is running on my localhost instead of H2, because of which my database drops first and then integration tests run. Even if I don't drop the database, it would still do modifications in main db records which is an issue in itself.
This is my application.properties file
spring.datasource.url=jdbc:mysql://localhost:3306/schema
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=none
spring.h2.console.enabled=true
logging.level.liquibase=info
spring.liquibase.contexts=${CONTEXT:some-context}
This is my application-test.properties file
spring.jpa.hibernate.ddl-auto=none
spring.datasource.name=testData
spring.jmx.default-domain=jpa.sample
liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
liquibase.enabled=true
liquibase.url=jdbc:h2:mem:testData;mode:MySQL
liquibase.user=sa
liquibase.password=
spring.liquibase.drop-first=true
This liquibase.change-log is path to a folder created in my project's resources folder. The only thing that I would like is for my integration tests to create an H2 database and perform all operations instead of referring to the MySQL running on my localhost which my application is using.
I have used this example as a reference: https://github.com/TurgayCan/spring-boot-liquibase-example
Let me know what I can do in this matter. Thank you.
EDIT:
I changed my application-test.properties to the file below, but the insert statements are not working now, however the table creation/alteration queries are running and on h2 instead of MySQL
spring.liquibase.contexts=${CONTEXT:nontest}
spring.datasource.url=jdbc:h2:mem:schema;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.name=schema
spring.jpa.properties.hibernate.default_schema=schema
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
I'd use the following configuration for application-test.properties:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.h2.console.enabled=true
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml

How to read application.properties from POM.xml

I'm not able to find out if it is possible to read any data in Spring's application.properties in the pom.xml file.
In my application-dev.properties file, I have an attribute:
google.project.id = 123456789
In my application-test.properties file, I have an attribute:
google.project.id = 987654321
In my application.properties file, I have an attribute. At one point I set it up for DEV at another time for TEST:
spring.profiles.active=dev
In my pom.xml file, I would like to read this project id according to the configuration in the spring.profiles.active attribute:
<configuration>
<projectId> PROJECT ID ???</projectId>
<version>1</version>
</configuration>
Please, could someone help me?
Thank you.
POM.xml is part of maven project build and has nothing to do with spring.
Spring framework has no idea about pom file.

loading data.sql and schema.sql on load with Spring boot 2.1.4 RELEASE

I've found couple of answers but unfortunately none of them is working for me.
My application properties is set to
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always
Problem: My resources/data.sql and resources/schema.sql is not loaded.
Wondering if there's another configuration setting that i'm missing.

Categories

Resources