In Spring Boot project I am trying to see in-memory tables from my IDE.
How to access in-memory h2 database from Intellij IDEA.
Here is a snippet from my application.yml:
h2:
datasource:
url: jdbc:h2:mem:mydb
username: username
password: 123
driver-class-name: org.h2.Driver
init-sql: h2.sql
console:
enabled: true
path: /search/console
settings:
trace: false
web-allow-others: false
Intellij has no field to input username for in-memory database:
Test Connection shows success, however it doesn't see tables from h2.sql.
I can access them using h2 console.
Just please bear in mind this may show you the db but not the table as they will only be visible in the h2 console. For you to access them through IntelliJ you may need to change the url and connection to be of a file type rather than in memory.
So instead of this:
#spring.datasource.url=jdbc:h2:mem:testdb
You'd have something like this:
spring.datasource.url=jdbc:h2:file:~/Users/yourUser/IdeaProjects/resume-portal/src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
And then after creating a datasource from url
You should now see both the database and its tables.
And that will still also be available from the console if you connect using the new url.
PS: If'd prefer to use a relative path you can change the url to be something similar to jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
Here is the application.yml file for the application.
spring.jpa.defer-datasource-initialization=true
#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
By the default, IntellJ doesn't show any of database.
right click at the datasource then choose Properties
At the Schemas tab, you'll see a list of options to choose which database should be shown (I normally choose All databases). Choose the database which you need it to be shown
The result:
I found another way. I created the H2 Server bean and added it to my SpringBoot Application. It looks like this:
// Start internal H2 server so can query from IDE
#Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
I also had the following on my application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.sql.init.mode=always
# Initialise H2 with H2GIS for spatial support ? see schema-h2.sql also
spring.sql.init.platform=h2
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
h2.tcp.enabled=true
In order to get my data.sql put into the tables in Intellij I had to make sure I used create-drop and added the spring.sql.init.mode=always
Note: I was using the H2GIS dialect - but this has not so far caused any issues
The connection setup in Intellij was still remote with this string jdbc:h2:tcp://localhost:9092/mem:testdb - dont forget to change the port to 9092
I the connection window in Intellij I also checked all tables under Schema. Then the tables are present for the H2 in mem database.
Related
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
I'm trying use to postgresql but i have this error.Here's my application properties
server.port=8081
application.name= = Address Registration
application.version = 1.0.0
spring.datasource.url= jdbc:postgresql://localhost:5432/addressRegistration
spring.datasource.username= postgres
spring.datasource.password= 12345
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
This can happen during the context loading when spring registers the beans required to finalize the program execution. There can be few options to check.
The dependency for h2 database is missing in your pom.xml. Hence the jar was not downloaded in the classpath.
Autoconfiguration is messed up. You can try with #EnableAutoConfiguration( exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
You might have some settings left with h2 database in your app.properties, however you don't want to use h2 database.
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.
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
I currently have a spring boot application which contains 2 configuration files: application.yaml and application-test.yaml. The application-test profile is loaded correctly and any settings in that file are working as expected.
However i am having an issue with one setting in particular which is spring.jpa.hibernate.ddl-auto = 'update'. When this setting is defined in the application.yaml file it causes my JPA unit tests to fail with the exception "Table "PG_CLASS" not found".
I have tried overriding this setting with different values in the application-test configuration file to no avail.
My full configuration files are as follows:
application.yaml
#Configure Postgres backend datasource
spring.datasource.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://ec2-54-75-233-146.eu-west-1.compute.amazonaws.com:5432/xxxx?user=xxxx&password=xxxxx&sslmode=require
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: false
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto: update
application-test.yaml
spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_default: false
spring.jpa.database-platform: org.hibernate.dialect.H2
Uncommenting the "spring.jpa.hibernate.ddl-auto: update" setting in my application.yaml file does fix the issue. But i would like to keep this enabled. Any ideas?
The above errror seems to be caused by Hibernate using the wrong dialect (PostgreSQL instead of H2).
Maybe try adding the following in application-test.yaml:
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.H2Dialect
These properties worked for me.
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create
spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.main.allow-bean-definition-overriding=true