├── .gitignore
├── .settings
└── .gitignore
├── README.md
├── build.gradle
├── pom.xml
└── src
└── main
├── java
└── com
│ └── alexbt
│ └── jpa
│ ├── JpaController.java
│ ├── Launcher.java
│ ├── Model.java
│ └── ModelJpaRepository.java
└── resources
├── application.properties
├── data-h2.sql
└── schema-h2.sql
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | /.classpath
3 | /.project
4 | /.idea
5 | /*.iml
6 | /.settings
--------------------------------------------------------------------------------
/.settings/.gitignore:
--------------------------------------------------------------------------------
1 | /org.eclipse.core.resources.prefs
2 | /org.eclipse.jdt.core.prefs
3 | /org.eclipse.m2e.core.prefs
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Spring Boot, Spring Data JPA and Embedded h2
2 | ---------------------------------------------------
3 |
4 | In almost all of my projects that involves external resources, I try my best to enable the application to fully run without dependencies.
5 | It's useful to provide a fully working backing
6 | This sample project shows how a spring-boot application can be setup with an embedded SQL database over JPA.
7 | The focus of this project is to show how to configure an embedded database with Spring Boot, however the source code also contains a RestController and a Spring Data Repository.
8 |
9 |
10 |
11 | Other sample projects with embedded databases
12 | ---------------------------------------------------
13 | * [sample-spring-boot-data-mongodb-embedded](https://github.com/alexturcot/sample-spring-boot-data-mongodb-embedded)
14 | * [sample-spring-boot-data-mongodb-embedded-multiple](https://github.com/alexturcot/sample-spring-boot-data-mongodb-embedded-multiple)
15 | * [sample-spring-boot-data-solr-embedded](https://github.com/alexturcot/sample-spring-boot-data-solr-embedded)
16 | * [sample-spring-boot-data-redis-embedded](https://github.com/alexturcot/sample-spring-boot-data-redis-embedded)
17 | * [sample-spring-boot-data-keyvalue-embedded](https://github.com/alexturcot/sample-spring-boot-data-keyvalue-embedded)
18 | * [sample-spring-boot-data-jpa-embedded](https://github.com/alexturcot/sample-spring-boot-data-jpa-embedded)
19 |
20 |
21 | Step by step
22 | ---------------------------------------------------
23 |
24 | **Maven dependencies**
25 | To load an embedded database with Spring Boot, all you really need is to add its maven dependency into your pom. The rest will be taken care of.
26 | In my case I chose h2, so I added the following dependency:
27 | ```
28 |
29 | com.h2database
30 | h2
31 |
32 | ```
33 |
34 | **Spring Boot configuration**
35 | ```
36 | spring.datasource.url=jdbc:h2:mem:TEST;MVCC=true;DB_CLOSE_DELAY=-1;MODE=Oracle
37 | spring.datasource.username=sa
38 | spring.datasource.password=
39 | spring.datasource.driver-class-name=org.h2.Driver
40 | spring.datasource.platform=h2
41 |
42 | spring.datasource.initialize=true
43 | #datasource.schema=
44 | #datasource.data
45 | spring.h2.console.enabled=true
46 | spring.jpa.hibernate.ddl-auto=none
47 | ```
48 |
49 | * **spring.datasource.***: sets up an in-memory H2 database;
50 | * **spring.datasource.initialize**: tells spring-boot to initialize the database with scripts;
51 | * **datasource.schema**: the schema sql script to load. By default it is schema-${platform}.sql then schema.sql;
52 | * **datasource.data**: the data sql script. By default, it is data-${platform}.sql then data.sql;
53 | * **spring.h2.console.enabled**: allow us to access the memory database from a web interface;
54 | * **spring.jpa.hibernate.ddl-auto**: hibernates also tries to initialize the database.
55 | When it detects an embedded database, it sets ddl-auto to create-drop and initialize the database with entities annotated with @Table (and also looks for imports.sql).
56 | This may lead to creating the same table twice. I prefer to stick with Spring Boot magic, so I set this to none
57 |
58 | **That's it**
59 |
60 | Assuming you have a Spring Boot entry point, launch it:
61 | ```
62 | @SpringBootApplication
63 | public class Launcher {
64 |
65 | public static void main(String[] args){
66 | new SpringApplicationBuilder() //
67 | .sources(Launcher.class)//
68 | .run(args);
69 | }
70 | }
71 | ```
72 | Then, you can access h2's console at: [localhost:8080/h2-console](http://localhost:8080/h2-console)
73 | Simply type in the url ```jdbc:h2:mem:TEST;MVCC=true;DB_CLOSE_DELAY=-1;MODE=Oracle``` in **JDBC URL** field
74 |
75 | It should work, however the database is empty.
76 |
77 | Now simply add sql script in src/main/resources.
78 | **schema-h2.sql**:
79 | ```
80 | CREATE TABLE MYTABLE
81 | (
82 | ID NUMBER(19) NOT NULL,
83 | VAL VARCHAR2(50) NOT NULL,
84 | );
85 | ```
86 | **data-h2.sql**:
87 | ```
88 | INSERT INTO MODEL (ID, VAL) VALUES (1, 'TEST');
89 | ```
90 | If you go back to the h2-console, you should see your data. All that is left to do, is to create your JPARepsitory like you would normally do with Spring Data, spring boot we'll wire it up with a datasource pointing to the embedded database.
91 |
92 | Get the code - do it
93 | ------------------------
94 | Clone the repository:
95 |
96 | $ git clone https://github.com/alexturcot/sample-spring-boot-data-jpa-embedded.git
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | version='1.0.0-SNAPSHOT'
2 | group='com.alexbt'
3 |
4 | buildscript {
5 | repositories {
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE',
10 | 'org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE'
11 | }
12 | }
13 |
14 | apply plugin: 'spring-boot'
15 |
16 | allprojects{
17 | apply plugin: 'io.spring.dependency-management'
18 | apply plugin: 'java'
19 | apply plugin: 'eclipse'
20 | apply plugin: 'idea'
21 | sourceCompatibility = 1.8
22 | targetCompatibility = 1.8
23 |
24 | tasks.withType(JavaCompile) {
25 | options.encoding = 'UTF-8'
26 | }
27 |
28 | dependencyManagement {
29 | imports {
30 | mavenBom 'io.spring.platform:platform-bom:2.0.6.RELEASE'
31 | }
32 | }
33 |
34 | repositories {
35 | mavenCentral()
36 | maven { url "http://repo.spring.io/release" }
37 | }
38 | }
39 |
40 | dependencies {
41 | compile 'org.springframework:spring-core',
42 | 'org.springframework.boot:spring-boot-starter-web',
43 | 'com.h2database:h2',
44 | 'org.springframework.boot:spring-boot-starter-data-jpa',
45 | 'org.springframework.boot:spring-boot-starter',
46 | 'org.springframework.data:spring-data-rest-webmvc'
47 | }
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.alexbt
6 | sample-spring-boot-data-jpa-embedded
7 | 1.0.0-SNAPSHOT
8 | jar
9 |
10 |
11 | org.springframework.boot
12 | spring-boot-starter-parent
13 | 1.3.5.RELEASE
14 |
15 |
16 | test
17 | http://maven.apache.org
18 |
19 |
20 | UTF-8
21 | 1.8
22 | ${java.version}
23 | ${java.version}
24 |
25 |
26 |
27 |
28 |
29 | io.spring.platform
30 | platform-bom
31 | 2.0.5.RELEASE
32 | pom
33 | import
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.data
41 | spring-data-rest-webmvc
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-starter
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-starter-web
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-starter-data-jpa
55 |
56 |
57 |
58 | com.h2database
59 | h2
60 |
61 |
62 |
63 | io.spring.platform
64 | platform-bom
65 | 2.0.6.RELEASE
66 | pom
67 | import
68 |
69 |
70 |
71 |
72 |
73 |
74 | org.apache.maven.plugins
75 | maven-jar-plugin
76 |
77 |
78 |
79 | org.springframework.boot
80 | spring-boot-maven-plugin
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/src/main/java/com/alexbt/jpa/JpaController.java:
--------------------------------------------------------------------------------
1 | package com.alexbt.jpa;
2 |
3 | import java.io.IOException;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @RequestMapping("/jpa")
13 | public class JpaController {
14 |
15 | @Autowired
16 | private ModelJpaRepository modelJpaRepository;
17 |
18 | @RequestMapping(path="/repo", method = RequestMethod.GET)
19 | public Iterable findByRepo() throws IOException {
20 | return modelJpaRepository.findAll();
21 | }
22 |
23 | @RequestMapping(value = "/repo/{value}", method = RequestMethod.GET)
24 | public void saveByRepo(@PathVariable String value) {
25 | Model model = new Model();
26 | model.setId(System.currentTimeMillis());
27 | model.setValue(value);
28 | modelJpaRepository.save(model);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/alexbt/jpa/Launcher.java:
--------------------------------------------------------------------------------
1 | package com.alexbt.jpa;
2 |
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.boot.builder.SpringApplicationBuilder;
5 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
6 |
7 | @SpringBootApplication
8 | @EnableJpaRepositories
9 | public class Launcher {
10 |
11 | public static void main(String[] args){
12 | new SpringApplicationBuilder() //
13 | .sources(Launcher.class)//
14 | .run(args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/alexbt/jpa/Model.java:
--------------------------------------------------------------------------------
1 | package com.alexbt.jpa;
2 |
3 | import javax.persistence.Column;
4 | import javax.persistence.Entity;
5 | import javax.persistence.GeneratedValue;
6 | import javax.persistence.Id;
7 | import javax.persistence.Table;
8 |
9 |
10 |
11 | @Entity
12 | @Table(name = "MODEL")
13 | public class Model {
14 |
15 | @Id
16 | @GeneratedValue
17 | @Column(name = "ID", nullable = false)
18 | private long id;
19 |
20 | @Column(name = "VAL", nullable = false)
21 | private String value;
22 |
23 | public long getId() {
24 | return id;
25 | }
26 |
27 | public void setId(long id) {
28 | this.id = id;
29 | }
30 |
31 | public String getValue() {
32 | return value;
33 | }
34 |
35 | public void setValue(String value) {
36 | this.value = value;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/alexbt/jpa/ModelJpaRepository.java:
--------------------------------------------------------------------------------
1 | package com.alexbt.jpa;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 |
5 | public interface ModelJpaRepository extends JpaRepository {
6 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #server.port=8080
2 | #server.contextPath=/
3 |
4 | spring.datasource.url=jdbc:h2:mem:TEST;MVCC=true;DB_CLOSE_DELAY=-1;MODE=Oracle
5 | spring.datasource.username=sa
6 | spring.datasource.password=
7 | spring.datasource.driver-class-name=org.h2.Driver
8 | spring.datasource.platform=h2
9 | spring.datasource.initialize=true
10 | #datasource.schema=classpath:schema.sql
11 | #datasource.data=classpath:data.sql
12 | #datasource.testdata=classpath:...
13 |
14 | spring.h2.console.enabled=true
15 | #spring.jpa.show-sql=true
16 | spring.jpa.hibernate.ddl-auto=none
17 | #spring.jpa.properties.hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
18 | #spring.profiles.include=application-embedded.properties.loaded,ldap.embedded
19 |
--------------------------------------------------------------------------------
/src/main/resources/data-h2.sql:
--------------------------------------------------------------------------------
1 | INSERT INTO MODEL (ID, VAL) VALUES (1, 'TEST');
2 |
--------------------------------------------------------------------------------
/src/main/resources/schema-h2.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE MODEL
2 | (
3 | ID NUMBER(19) NOT NULL,
4 | VAL VARCHAR2(50) NOT NULL,
5 | );
6 |
--------------------------------------------------------------------------------