Skip to content

Commit c5d8150

Browse files
author
Dave Syer
committed
Add docs on initializing a database
Fixes spring-projectsgh-364
1 parent e663b44 commit c5d8150

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: docs/howto.md

+63
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,69 @@ See
856856
[`JpaBaseConfiguration`](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java)
857857
for the default settings.
858858
859+
## Initialize a Database
860+
861+
An SQL database can be initialized in different ways depending on what
862+
your stack is. Or of course you can do it manually as long as the
863+
database is in a server.
864+
865+
### JPA
866+
867+
JPA has features for DDL generation, and these can be set up to
868+
run on startup against the database. This is controlled through two
869+
external properties:
870+
871+
* `spring.jpa.generate-ddl` (boolean) switches the feature on and off
872+
and is vendor independent
873+
* `spring.jpa.hibernate.ddl-auto` (enum) is a Hibernate feature that
874+
controls the behaviour in a more fine-grained way. See below for
875+
more detail.
876+
877+
### Hibernate
878+
879+
You can set `spring.jpa.hibernate.ddl-auto` explicitly and the
880+
standard Hibernate property values are "none", "validate", "update",
881+
"create-drop". Spring Boot chooses a default value for you based on
882+
whether it thinks your database is embedded (default "create-drop") or
883+
not (default "none"). An embedded database is detected by looking at
884+
the `Connection` type: `hsqldb`, `h2` and `derby` are embedded, the
885+
rest are not. Be careful when switching from in-memory to a "real"
886+
database that you don't make assumptions about the existence of the
887+
tables and data in the new platform. You either have to set "ddl-auto"
888+
expicitly, or use one of the other mechanisms to initialize the
889+
database.
890+
891+
In addition, a file named "import.sql" in the root of the classpath
892+
will be executed on startup. This can be useful for demos and for
893+
testing if you are carefuil, but probably not something you want to be
894+
on the classpath in production. It is a Hibernate feature (nothing to
895+
do with Spring).
896+
897+
### Spring JDBC
898+
899+
Spring JDBC has a `DataSource` initializer feature. Spring Boot
900+
enables it by default and loads SQL from the standard locations
901+
`schema.sql` and `data.sql` (in the root of the classpath). In
902+
addition Spring Boot will load a file `schema-${platform}.sql` where
903+
`platform` is the vendor name of the database (`hsqldb`, `h2,
904+
`oracle`, `mysql`, `postgresql` etc.). Spring Boot *disables* the
905+
failfast feature of the Spring JDBC initializer, so if the scripts
906+
cause exceptions they will be logged, but the application will still
907+
start. This is so that they can be used as "poor man's migrations"
908+
(inserts that fail mean that the data is already there, so no need to
909+
fail for instance), but it does mean that you need to test the state
910+
of your database on startup if you want to be sure that it was
911+
successful (or else monitor the Spring JDBC DEBUG logs).
912+
913+
### Higher Level Migration Tools
914+
915+
Spring Boot works fine with higher level migration tools
916+
[Flyway](http://flywaydb.org/) (SQL-based) and
917+
[Liquibase](http://www.liquibase.org/) (XML). In general we prefer
918+
Flyway because it is easier on the eyes, and it isn't very common to
919+
need platform independence: usually only one or at most couple of
920+
platforms is needed.
921+
859922
<span id="discover.options"/>
860923
## Discover Built-in Options for External Properties
861924

0 commit comments

Comments
 (0)