Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
c5d8150f
Commit
c5d8150f
authored
Feb 19, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add docs on initializing a database
Fixes gh-364
parent
e663b44f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
0 deletions
+63
-0
howto.md
docs/howto.md
+63
-0
No files found.
docs/howto.md
View file @
c5d8150f
...
...
@@ -856,6 +856,69 @@ See
[
`JpaBaseConfiguration`
](
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java
)
for the default settings.
## Initialize a Database
An SQL database can be initialized in different ways depending on what
your stack is. Or of course you can do it manually as long as the
database is in a server.
### JPA
JPA has features for DDL generation, and these can be set up to
run on startup against the database. This is controlled through two
external properties:
*
`spring.jpa.generate-ddl`
(boolean) switches the feature on and off
and is vendor independent
*
`spring.jpa.hibernate.ddl-auto`
(enum) is a Hibernate feature that
controls the behaviour in a more fine-grained way. See below for
more detail.
### Hibernate
You can set
`spring.jpa.hibernate.ddl-auto`
explicitly and the
standard Hibernate property values are "none", "validate", "update",
"create-drop". Spring Boot chooses a default value for you based on
whether it thinks your database is embedded (default "create-drop") or
not (default "none"). An embedded database is detected by looking at
the
`Connection`
type:
`hsqldb`
,
`h2`
and
`derby`
are embedded, the
rest are not. Be careful when switching from in-memory to a "real"
database that you don't make assumptions about the existence of the
tables and data in the new platform. You either have to set "ddl-auto"
expicitly, or use one of the other mechanisms to initialize the
database.
In addition, a file named "import.sql" in the root of the classpath
will be executed on startup. This can be useful for demos and for
testing if you are carefuil, but probably not something you want to be
on the classpath in production. It is a Hibernate feature (nothing to
do with Spring).
### Spring JDBC
Spring JDBC has a
`DataSource`
initializer feature. Spring Boot
enables it by default and loads SQL from the standard locations
`schema.sql`
and
`data.sql`
(in the root of the classpath). In
addition Spring Boot will load a file
`schema-${platform}.sql`
where
`platform`
is the vendor name of the database (
`hsqldb`
,
`h2,
`
oracle
`, `
mysql
`, `
postgresql
` etc.). Spring Boot *disables* the
failfast feature of the Spring JDBC initializer, so if the scripts
cause exceptions they will be logged, but the application will still
start. This is so that they can be used as "poor man's migrations"
(inserts that fail mean that the data is already there, so no need to
fail for instance), but it does mean that you need to test the state
of your database on startup if you want to be sure that it was
successful (or else monitor the Spring JDBC DEBUG logs).
### Higher Level Migration Tools
Spring Boot works fine with higher level migration tools
[Flyway](http://flywaydb.org/) (SQL-based) and
[Liquibase](http://www.liquibase.org/) (XML). In general we prefer
Flyway because it is easier on the eyes, and it isn't very common to
need platform independence: usually only one or at most couple of
platforms is needed.
<span id="discover.options"/>
## Discover Built-in Options for External Properties
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment