Take your skills to the next level!
The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, 2 monthly Q&A calls, monthly coding challenges, a community of like-minded developers, and regular expert sessions.
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.
I need to create a database and initialize it with a set of test data. Is there an easy way to do that with Hibernate?
Hibernate implements the JPA standard which defines a set of configuration parameters to setup a database. The following code snippet shows an example of such a configuration in the persistence.xml file.
<persistence> <persistence-unit name="my-persistence-unit" transaction-type="JTA"> … <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.create-source" value="script"/> <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/> <property name="javax.persistence.schema-generation.drop-source" value="script"/> <property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/> <property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/> </properties> </persistence-unit> </persistence>
OK, this configuration might look overwhelming at the beginning, but it’s pretty simple and flexible as soon as you know the different parameters.
It tells Hibernate to use 2 SQL scripts to drop an existing database, to create a new and to run an SQL script afterward to initialize it. Let’s have a look at the different configuration parameters:
- persistence.schema-generation.database.action = drop-and-create tells Hibernate to first drop and then create a new database,
- persistence.schema-generation.create-source = script and javax.persistence.schema-generation.drop-source = script define that Hibernate shall use a script to create and drop the database and
- persistence.schema-generation.create-script-source and javax.persistence.schema-generation.drop-script-source specify the location of these scripts and
- persistence.sql-load-script-source provides the path the to the SQL script which Hibernate shall use to initialize the database.
As you have seen, the configuration isn’t too complicated. The parameters follow an easy to understand the concept, and they provide the required flexibility to create a configuration that fits your application.
These are not the only parameters you can use to create and initialize your database. I explained all of them in the post: Standardized schema generation and data loading with JPA 2.1.
Hibernate Tips Book
Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching, and statically and dynamically defined queries.