Hibernate Tips: How to create and initialize a database

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.

Question:

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?

Solution:

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.


Learn more:

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.

Get it now!


Related Articles

Responses

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. How do you fill the tables with test data for integration tests?

  2. As a developr, helpful. As a DBA, terrified! That code could suddenly drop and re-create tables on the fly? If that code accidently made production it would be horrific. Always make sure that code is only in test suite software. And anyone with DBA skills, make sure you remove any DDL permissions in production from the application (hopefully you can do this) so DDL changes can be applied during deploys rather than through the application and thus allowing for drops and what not. (I realize that DDL permissions sometimes have to be given for vendor software. But avoid it if you can.) Just my two cents since I work in both worlds (Java developer and DBA).

    1. Hi Chris,

      you’re right!
      This feature is for tests but you should never deploy it to production. But that’s the case for all tools you use to re-create your test database 😉

      Regards,
      Thorben

  3. Great tip!

    Don’t you recommend to use Hibernate old configuration (hibernate.hbm2ddl.auto=update)?