Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
IN 28MINUTES
MINUTES
Free
Free Course
Course All
All Courses
Courses Write
Write For
For Us
Us
This guide will help you understand the concept of in memory database. We will look
at simple JPA example to understand the best practices in using in memory databases.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
10 Step Reference Courses
Spring
Spring Framework
Framework for
for Beginners
Beginners in
in 10
10 Steps
Steps
Spring
Spring Boot
Boot for
for Beginners
Beginners in
in 10
10 Steps
Steps
Spring
Spring MVC
MVC in
in 10
10 Steps
Steps
JPA
JPA and
and Hibernate
Hibernate in
in 10
10 Steps
Steps
Eclipse
Eclipse Tutorial
Tutorial for
for Beginners
Beginners in
in 55 Steps
Steps
Maven
Maven Tutorial
Tutorial for
for Beginners
Beginners in
in 55 Steps
Steps
JUnit
JUnit Tutorial
Tutorial for
for Beginners
Beginners in
in 55 Steps
Steps
Mockito
Mockito Tutorial
Tutorial for
for Beginners
Beginners in
in 55 Steps
Steps
Complete
Complete in28Minutes
in28Minutes Course
Course Guide
Guide
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Setup the tables
Populate the data
Connect the application to the database by setting up a data source and a lot of
other code
Scenario 1 - Let’s consider a situation where you would want to do a quick POC. Using
a traditional database involves a lot of overhead.
You don’t want them to fail when some data/schema in the database changes
You would want to be able to run them in parallel - multiple developers might be
running the tests in parallel.
Advantages
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
H2
H2 is one of the popular in memory databases. Spring Boot has very good integration
for H2.
From https://en.wikipedia.org/wiki/H2_(DBMS)
In most situations, just adding the H2 runtime jar into dependencies should be
su cient.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Launch Spring Initializr and choose the following
Choose com.in28minutes.springboot.rest.example as Group
Choose spring-boot-2-jpa-with-hibernate-and-h2 as Artifact
Choose following dependencies
Web
JPA
H2
DevTools
@Entity
public class Student {
@Id
@GeneratedValue
private Long id;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
private String name;
private String passportNumber;
H2 - A Few Tips
An in-memory database is live only during the time of execution of the
application. It is an e cient way to learn a framework.
This is not how you want your real world applications to behave.
We explain how to connect to a database of your choice in the answer to the
question “How do we connect to a external database?”.
/src/main/resources/application.properties
# Enabling H2 Console
spring.h2.console.enabled=true
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
You will see that a new table called ‘student’ is created in H2 Console.
Its because of Spring Boot Auto Con guration. We will talk about this in the next
section.
You can also populate some data into student table by adding a le called data.sql
/src/main/resources/data.sql
When you reload the application, you can launch up H2 Console to see that the
student table is populated with the data from `data.sql’
http://localhost:8080/h2-console.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
How did all the magic happen? Let’s look at it question by question in the next section.
If you are talking to an in memory db, by default, it looks at the entities and creates
the database and the tables.
However, if you connect to a mysql database, Spring Boot knows that its a permanent
database. By default, it expects you to set up the database, set up the tables and it uses
the connection that you established.
Q : How did the Spring Boot Applica on connect to the database H2?
Its down to Spring Boot Auto Con guration!
First thing you would need to understand is Spring Boot Auto Con guration.
http://www.springboottutorial.com/spring-boot-auto-con guration
As far as H2 is concerned, as soon as Spring Boot sees H2 in the class path, it auto
con gures a data source similar to what you see below:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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
It knows that you are using an inmemory database H2 and it uses the default url if you
don’t provide one.
From https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-
auto-con guration.html
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
con gured any database connection beans, then Spring
Boot will auto-con gure an in-memory database.
Recommended Reading
http://www.springboottutorial.com/spring-boot-auto-con guration
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
H2 is an in memory database. Its not a persisted database.
In the browser, change the database url to jdbc:h2:mem:testdb (Shown in the screen
below).
spring.h2.console.enabled=true
Q : How did the insert query from data.sql run at applica on startup?
That’s part of the Spring Boot startup routine. Any queries in data.sql are run at
application startup. You can read more here.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-
database-initialization.html
With this con guration, the data is not lost even after spring boot restart and
computer restart.
You would nd H2 being very rarely used in this way. If you are really interested in a
persistent database, we recommend exploring MySQL, Oracle or some other relational
database.
application.properties
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
spring.datasource.name=yourdbname
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.initialize=false
spring.datasource.url=jdbc:h2:file:~/yourdbname;DB_CLOSE_ON_EXIT=FAL
spring.jpa.hibernate.ddl-auto = update
If we want to use di erent properties for tests, then we can override the properties le
in the main folder by placing another le with the same name in src/test/resources.
First add the dependencies for your database driver (mysql in the example below) and
make the dependency for h2 test scoped.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
src\main\resources\application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/person_example
spring.datasource.username=personuser
spring.datasource.password=YOUR_PASSWORD
src\test\resources\application.properties
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Congratulations! You are reading an article from a series
of 50+ articles on Spring Boot and Microservices. We also
have 20+ projects on our Github repository. For the
complete series of 50+ articles and code examples, click
here
here.
Next Steps
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Join 100,000 Learners and Become a Spring Boot Expert - 55 Awesome
Awesome Courses
Courses on
on
Microservices,
Microservices, API’s,
API’s, Web
Web Services
Services with
with Spring
Spring and
and Spring
Spring Boot.
Boot. Start
Start Learning
Learning
Now
Now
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Learn Basics of Spring Boot - Spring
Spring Boot
Boot vs
vs Spring
Spring vs
vs Spring
Spring MVC
MVC,
MVC Auto
Auto
Con
Con guration
guration,
guration Spring
Spring Boot
Boot Starter
Starter Projects
Projects,
Projects Spring
Spring Boot
Boot Starter
Starter Parent
Parent,
Parent Spring
Spring
Boot
Boot Initializr
Initializr
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Join our free Spring Boot in 10 Steps Course.
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
By default, the console is available at /h2-console. You can customize the console’s path by using
the spring.h2.console.path property.
△ ▽ • Reply • Share ›
Microservices with Spring Boot - Part 3 - Creating a SOAP Web Service with Spring Boot
Creating Currency Conversion Microservice Starter Web Services
4 comments • a year ago 2 comments • a year ago
M.I. — Hmm.. just wondering how clumsy the micro Ivo — Nice article. The only trouble I had was not
service solution becomes; While trying to make realizing that for SpringBoot to handle properly
things simple, more boilerplate code is added. And autodiscovery I need to put WebServiceConfig into
Creating a CRUD REST API/Service with Spring Spring Boot and Content Negotiation - XML and
Boot, JPA and Hibernate JSON Representations
7 comments • a year ago 1 comment • a year ago
Throyer — use JpaRepository, because it returns a Cherry Reddy — i am trying to do transform for
list XML to JSON. But i am just a beginner in Java, Can
anyone post the sample code?
Related Posts
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Spring
Spring Boot
Boot Tutorials
Tutorials for
for Beginners
Beginners
At
At in28Minutes,
in28Minutes, we
we are
are creating
creating aa number
number of
of tutorials
tutorials with
with videos,
videos, articles
articles &
&
courses
courses on
on Spring
Spring Boot
Boot for
for Beginners
Beginners and
and Experienced
Experienced Developers.
Developers. This
This resources
resources
will
will help
help you
you learn
learn and
and gain
gain expertise
expertise at
at Spring
Spring Boot.
Boot.
20+
20+ Spring
Spring Boot
Boot Projects
Projects with
with Code
Code Examples
Examples
At
At in28Minutes,
in28Minutes, we
we have
have created
created more
more than
than 20
20 projects
projects with
with code
code examples
examples on
on
Github.
Github. We
We have
have 50+
50+ articles
articles explaining
explaining these
these projects.
projects. These
These code
code examples
examples will
will
you
you learn
learn and
and gain
gain expertise
expertise at
at Spring
Spring Boot.
Boot.
Microservices
Microservices with
with Spring
Spring Boot
Boot -- Part
Part 55 -- Using
Using Eureka
Eureka Naming
Naming Server
Server
Let's
Let's learn
learn the
the basics
basics of
of microservices
microservices and
and microservices
microservices architectures.
architectures. We
We will
will also
also
start
start looking
looking at
at aa basic
basic implementation
implementation ofof aa microservice
microservice with
with Spring
Spring Boot.
Boot. We
We will
will
create
create aa couple
couple of
of microservices
microservices and
and get
get them
them to to talk
talk to
to each
each other
other using
using Eureka
Eureka
Naming
Naming Server
Server and
and Ribbon
Ribbon for
for Client
Client Side
Side Load
Load Balancing.
Balancing. In
In part
part 55 of
of this
this series,
series, we
we
will
will focus
focus on
on enabling
enabling Eureka
Eureka Naming
Naming Server
Server and
and have
have the
the microservices
microservices
communicate
communicate with
with it.
it.
Microservices
Microservices with
with Spring
Spring Boot
Boot -- Part
Part 44 -- Using
Using Ribbon
Ribbon for
for Load
Load Balancing
Balancing
Let's
Let's learn
learn the
the basics
basics of
of microservices
microservices and
and microservices
microservices architectures.
architectures. We
We will
will also
also
start
start looking
looking at
at aa basic
basic implementation
implementation ofof aa microservice
microservice with
with Spring
Spring Boot.
Boot. We
We will
will
create
create aa couple
couple of
of microservices
microservices and
and get
get them
them to to talk
talk to
to each
each other
other using
using Eureka
Eureka
Naming
Naming Server
Server and
and Ribbon
Ribbon for
for Client
Client Side
Side Load
Load Balancing.
Balancing. In
In part
part 4
4 of
of this
this series,
series, we
we
will
will focus
focus on
on using
using Ribbon
Ribbon to
to do
do Load
Load Balancing.
Balancing.
Microservices
Microservices with
with Spring
Spring Boot
Boot -- Part
Part 33 -- Crea
Crea ng
ng Currency
Currency Conversion
Conversion Microservice
Microservice
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Let's
Let's learn
learn the
the basics
basics of
of microservices
microservices and
and microservices
microservices architectures.
architectures. We
We will
will also
also
start
start looking
looking at
at aa basic
basic implementation
implementation of
of aa microservice
microservice with
with Spring
Spring Boot.
Boot. We
We will
will
create
create aa couple
couple of
of microservices
microservices and
and get
get them
them to
to talk
talk to
to each
each other
other using
using Eureka
Eureka
Naming
Naming Server
Server and
and Ribbon
Ribbon for
for Client
Client Side
Side Load
Load Balancing.
Balancing. In
In part
part 33 of
of this
this series,
series, we
we
will
will focus
focus on
on Creating
Creating Currency
Currency Conversion
Conversion Microservice.
Microservice.
Microservices
Microservices with
with Spring
Spring Boot
Boot -- Part
Part 22 -- Crea
Crea ng
ng Forex
Forex Microservice
Microservice
Let's
Let's learn
learn the
the basics
basics of
of microservices
microservices and
and microservices
microservices architectures.
architectures. We
We will
will also
also
start
start looking
looking at
at aa basic
basic implementation
implementation of
of aa microservice
microservice with
with Spring
Spring Boot.
Boot. We
We will
will
create
create aa couple
couple of
of microservices
microservices and
and get
get them
them to
to talk
talk to
to each
each other
other using
using Eureka
Eureka
Naming
Naming Server
Server and
and Ribbon
Ribbon for
for Client
Client Side
Side Load
Load Balancing.
Balancing. In
In part
part 22 of
of this
this series,
series, we
we
will
will focus
focus on
on Creating
Creating Forex
Forex Microservice.
Microservice.
Microservices
Microservices with
with Spring
Spring Boot
Boot -- Part
Part 11 -- Ge
Ge ng
ng Started
Started
Let's
Let's learn
learn the
the basics
basics of
of microservices
microservices and
and microservices
microservices architectures.
architectures. We
We will
will also
also
start
start looking
looking at
at aa basic
basic implementation
implementation ofof aa microservice
microservice with
with Spring
Spring Boot.
Boot. We
We will
will
create
create aa couple
couple of
of microservices
microservices and
and get
get them
them to to talk
talk to
to each
each other
other using
using Eureka
Eureka
Naming
Naming Server
Server and
and Ribbon
Ribbon for
for Client
Client Side
Side Load
Load Balancing.
Balancing. In
In part
part 11 of
of this
this series,
series,
lets
lets get
get introduced
introduced to
to the
the concept
concept of
of microservices
microservices and
and understand
understand how
how to
to create
create
great
great microservices
microservices with
with Spring
Spring Boot
Boot and
and Spring
Spring Cloud.
Cloud.
Ge
Ge ng
ng Started
Started with
with Spring
Spring Boot
Boot -- 77 things
things aa beginner
beginner should
should know!
know!
Spring
Spring Boot
Boot is
is aa tricky
tricky framework
framework to
to understand.
understand. In
In this
this tutorial
tutorial for
for beginnners
beginnners with
with
Spring
Spring Boot,
Boot, we
we will
will look
look at
at the
the basics
basics of
of Spring
Spring Boot
Boot and
and help
help you
you understand
understand the
the
important
important concepts
concepts - - Architecture,
Architecture, Starter
Starter Projects,
Projects, Auto
Auto Con
Con guration
guration and
and Starter
Starter
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Parent.
Parent. We
We will
will look
look at
at the
the architecture
architecture of
of Spring
Spring Boot
Boot and
and explore
explore aa variety
variety of
of
resources
resources -
- tutorials,
tutorials, courses
courses and
and videos
videos -
- we
we created
created to
to help
help you
you get
get started
started with
with
Spring
Spring Boot
Boot ee ectively.
ectively.
Spring
Spring Boot
Boot REST
REST API
API Projects
Projects with
with Code
Code Examples
Examples
At
At in28Minutes,
in28Minutes, we
we have
have created
created 8
8 Spring
Spring Boot
Boot REST
REST API
API projects
projects with
with code
code
examples
examples on
on Github.
Github. We
We have
have 10+
10+ tutorial
tutorial articles
articles explaining
explaining these
these projects.
projects. These
These
code
code examples
examples will
will help
help beginners
beginners and
and experts
experts toto learn
learn and
and gain
gain expertise
expertise at
at
developing
developing RESTful
RESTful Services
Services with
with Spring
Spring Boot.
Boot.
Crea
Crea ng
ng aa SOAP
SOAP Web
Web Service
Service with
with Spring
Spring Boot
Boot Starter
Starter Web
Web Services
Services
Let's
Let's learn
learn how
how to
to create
create aa SOAP
SOAP Web
Web Service
Service with
with Spring
Spring Boot
Boot Starter
Starter Web
Web Services.
Services.
We
We will
will take
take aa Contract
Contract First
First approach
approach by
by de
de nining
nining an
an XSD
XSD and
and exposing
exposing aa WSDL
WSDL
from
from it.
it.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD