How to populate Java (web) application with initial data using Spring/JPA/Hibernate

Posted by Tuukka Mustonen on Stack Overflow See other posts from Stack Overflow or by Tuukka Mustonen
Published on 2010-03-11T08:24:09Z Indexed on 2010/03/11 21:24 UTC
Read the original article Hit count: 853

Filed under:
|
|
|
|

I want to setup my database with initial data programmatically. I want to populate my database for development runs, not for testing runs (it's easy). The product is built on top of Spring and JPA/Hibernate.

  • Developer checks out the project
  • Developer runs command/script to setup database with initial data
  • Developer starts application (server) and begins developing/testing

then:

  • Developer runs command/script to flush the database and set it up with new initial data because database structures or the initial data bundle were changed

What I want is to setup my environment by required parts in order to call my DAOs and insert new objects into database. I do not want to create initial data sets in raw SQL, XML, take dumps of database or whatever. I want to programmatically create objects and persist them in database as I would in normal application logic.

One way to accomplish this would be to start up my application normally and run a special servlet that does the initialization. But is that really the way to go? I would love to execute the initial data setup as Maven task and I don't know how to do that if I take the servlet approach.

There is somewhat similar question. I took a quick glance at the suggested DBUnit and Unitils. But they seem to be heavily focused in setting up testing environments, which is not what I want here. DBUnit does initial data population, but only using xml/csv fixtures, which is not what I'm after here. Then, Maven has SQL plugin, but I don't want to handle raw SQL. Maven also has Hibernate plugin, but it seems to help only in Hibernate configuration and table schema creation (not in populating db with data).

How to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about web-applications