Spring H2 Support: Difference between revisions
(10 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[Spring_Data#Supported_SQL_Databases|Spring Data]] | * [[Spring_Data#Supported_SQL_Databases|Spring Data]] | ||
* [[Spring_Data_JPA#Add_Specific_Database_Support|Spring Data JPA]] | |||
* [[H2]] | * [[H2]] | ||
Line 9: | Line 10: | ||
=Overview= | =Overview= | ||
If a H2 dependency is available in the runtime classpath, the [[Spring Boot]] runtime automatically creates an appropriate DataSource bean in the application context. The bean applies the SQL scripts "schema.sql" and "data.sql". | |||
=Simple Project= | =Simple Project= | ||
Line 25: | Line 28: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Spring Configuration | Follow the JPA programming model described at [[Spring_Data_JPA#Adding_Persistence_to_an_Application_with_Spring_Data_JPA|Adding Persistence with Spring Data JPA]]. | ||
To test from command line, add command-line support as specified in [[Console_Application_with_Spring_Boot|Console (command-line) Application with Spring Boot]]. | |||
To configure IntelliJ IDEA to access the in-memory database see [[IntelliJ_IDEA_H2_Support|IntelliJ IDEA H2 Support]]. | |||
=<span id='Spring_Configuration'></span>Database Access Configuration= | |||
Based on: {{Internal|Spring_Data_JPA#Database_Access_Configuration|Spring Data JPA - Database Access Configuration}} | |||
Add in [[application.properties]] or equivalent: | Add in [[application.properties]] or equivalent: | ||
Line 34: | Line 45: | ||
spring.datasource.password= | spring.datasource.password= | ||
spring.datasource.platform=h2 | spring.datasource.platform=h2 | ||
</syntaxhighlight> | |||
=Database Initialization= | |||
Based on: {{Internal|Spring_Data_JPA#Database_Initialization|Spring Data JPA - Database Initialization}} | |||
<syntaxhighlight lang='text'> | |||
spring.jpa.generate-ddl=true | |||
</syntaxhighlight> | |||
Alternative: | |||
<syntaxhighlight lang='text'> | |||
spring.datasource.initialization-mode=always | spring.datasource.initialization-mode=always | ||
spring.jpa.hibernate.ddl-auto=none | spring.jpa.hibernate.ddl-auto=none |
Latest revision as of 18:14, 8 December 2018
Internal
Playground Example
Overview
If a H2 dependency is available in the runtime classpath, the Spring Boot runtime automatically creates an appropriate DataSource bean in the application context. The bean applies the SQL scripts "schema.sql" and "data.sql".
Simple Project
Use Spring Initializr to bootstrap the project. Alternatively use Spring Initializr-generated build.gradle and settings.gradle.
To bootstrap JPA and H2, add the following dependencies:
dependencies {
...
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtimeOnly('com.h2database:h2')
...
}
Follow the JPA programming model described at Adding Persistence with Spring Data JPA.
To test from command line, add command-line support as specified in Console (command-line) Application with Spring Boot.
To configure IntelliJ IDEA to access the in-memory database see IntelliJ IDEA H2 Support.
Database Access Configuration
Based on:
Add in application.properties or equivalent:
spring.datasource.url=jdbc:h2:tcp://localhost/mem:default
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
Database Initialization
Based on:
spring.jpa.generate-ddl=true
Alternative:
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true