Spring-boot populate H2 database with schema.sql and data.sql - spring-boot

I set up Spring-boot to work with H2 in-memory database
application.properties file is in the /config directory
and it looks like, this file is processed
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/myconsole
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=data.sql
This file is processed and the console appears at /myconsole
But the schema.sql and data.sql are not processed and db is empty.
I placed schema.sql and data.sql files both under /config and /src/main/resources.
SQL language instructions are correct and I can populate the table using console input.
Another strange thing is
even though i name db as
spring.datasource.url=jdbc:h2:mem:mydb
the spring console loads another database testdb
o.s.j.d.e.EmbeddedDatabaseFactory --- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
How to load the H2 database correctly?

Resolved the issue.
The spring boot app requires its ownd jdbc dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
NON-boot dependency, that I had, is NOT enough alone:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Without the "spring-boot-starter-jdbc" dependency
"spring.datasource.url" settings in file "application.properties"
are not processed.
That file is actually processed, but not the jdbc settings.
Spring boot will create its own testdb in the memory, which destroys the data
after closing the application.

Related

Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring does not find application properties

I have created a small application with spring boot that access to a database (mySql) using spring data:
spring-boot-starter-parent: 2.4.2
spring-boot-starter-web: 2.4.2
spring-boot-starter-data-jpa: 2.4.2
mysql-connector-java: 8.0.22
spring-boot-starter-test: 2.4.2
org.junit.jupiter: 5.7.0
junit-jupiter-engine: 5.7.0
I have configured my application.properties, inside src/main/resources, and I have configured the properties for url, username, password, driver, ..., like this:
spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
I have an interface for my repository for access to my database:
#Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {
}
also, I have defined my entity.
The issue consists on when I start my webapp, I get an error because it does not read the parameters for configuring the datasource from the application.properties:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following: If you want an embedded database (H2, HSQL or
Derby), please put it on the classpath. If you have database settings
to be loaded from a particular profile you may need to activate it (no
profiles are currently active).
If I remove the dependencies for spring-boot-starter-test and junit, the application loads fine, and it access to database. But if I add the dependency for adding my junits, it does not read my application.properties file and I get the previous error.
How can I have my application configured with junits and reading the configuration for my application with the application.properties file?
Thank you in advance!
Your test/application.properties needs to be configured as well. Try adding this to your test application properties file
spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
and this to your pom if you don't already support h2.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
check your application.properties
changing
spring.datasource.driver=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Spring data jpa format_sql does not format query

I am working on a spring data jpa project and need some help formatting the sql statements which are being logged. Here are the properties I used and tried, some are working, others are ignored as indicated.
Also some other stuff I have tried
#works
spring.jpa.hibernate.ddl-auto=validate
#works
spring.jpa.show-sql=true
#ignored
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate_format_sql=true
spring.jpa.properties.format_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.format_sql=true
#ignored
spring.jpa.properties.hibernate.type=trace
I found on several stackoverflow questions and other sites saying that " spring.jpa.properties.hibernate.format_sql=true " should work, but here it is ignored.
In the documentation in the list of properties it is not listed: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.data
According to https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#data.sql.jpa-and-spring-data.creating-and-dropping hibernate properties should be set with spring.jpa.properties.hibernate...
The project, does not have other hibernate configuration except the hibernate core dependency in pom.xml , which is needed . Here are the two relevant dependencies in pom:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
spring data jpa is version 2.5.6 , hibernate 5.4.32
Is there anything I can do to be able to format the sql statements with spring.jpa properties ?
Thank you!
LE: I have debugged the code and saw that the entitymanager has the hibernate.format_sql=true property.

How to "allow remote database creation" when using h2 database?

I'm trying to create a Spring Boot project with H2 database, which would be accessible by other programs.
application.properties
spring.datasource.url = jdbc:h2:tcp://localhost:8084/~/./db/tech
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.initialization-mode=always
SpringBootMyApplication.java
#SpringBootApplication
public class SpringBootMyApplication{
public static void main(String[] args) {
SpringApplication.run(SpringBootMyApplication.class, args);
}
#Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "8084");
}
}
The exception is:
Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/onz03589/db/tech" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200]
How to actually "allow remote database creation"?
You need to add "-ifNotExists" parameter to Server.createTcpServer(). But, again, you shouldn't use it together with "-tcpAllowOthers" unless your port is guarded somehow.
You need to add spring boot starter jpa dependency to enable autoconfiguration for h2 database setup.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
I added these two dependencies in the pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
And added below in the Application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
And it worked for me.
If you have any tables or data to be inserted while creating database. You can do it by putting .sql files under resources directory
Now, you can open your database on
http://localhost:port/h2-console
The reason might be
After you run the server and hit the console the spring.datasource.url is not populated properly. Copy paste the url from application.properties file
You might have made mistake in properties uppercase and lowercase
e.g. I have used
spring.datasource.driverclassname
instead of
spring.datasource.driverClassName
So check the casesensitive property names

Active profile ignored

Sample code
I have already added "spring.profiles.active=local" in application.properties file
But while running the application it is not able to recognize the profile and running with default profile
application.properties
# Tomcat Server Config
server.port=8071
#Profile Config
spring.profiles.active=local
application-local.properties
# Spring JPA Config
spring.datasource.url=jdbc:postgresql://localhost:5432/User
spring.datasource.username=postgres
spring.datasource.password=amt123`
# Hibernate Config
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
"spring.devtools.add-properties" default value is true
Once it is set to false, spring boot disables profile configuration.
For me even if I set the value to true, still profile configuration was not enabled
I fixed this issue by removing the dev-tool dependency and adding it again.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
I am not sure on the internal workflow.
If anyone has a proper explanation for this, please comment

h2database login screen not working in my spring application

I am using h2dabase as in memory database in my application.
I have added following dependency in my pom.xml file.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Following is my application.properties
spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
Following is the error I am getting on the browser:
To reproduce the error, I've downloaded a fresh Spring boot project with the spring-boot-starter-web and h2 modules. When using your settings, the URL was inaccessible just like your screenshot showed.
The URL did became accessible when I added
spring.h2.console.path=/h2-console to application.properties.

Resources