Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate web application with initial data

I am writing web application and am wondering what the recommended way is to populate initial data. This is JPA/Hibernate and Spring application and is built by maven. Up to now I've used script which populate database with initial data, started by hand.

Unit tests work with theirs own data, created in code before each test. My test classes extends org.springframework.test.jpa.AbstractJpaTests.

Finally I'd like to have a maven build which create database form entities (now I need to run application first time to create database, then run script) then populates initial/dictionary data and run unit and integration tests. This process should be fully automated to put this build in CI server (hudson), creating new database from scratch is also appreciate.

Additional requirement: Database agnostic solution will be most valuable.

EDIT: Best example of what I'm looking for is init closure in BootStrap.groovy.

like image 592
cetnar Avatar asked Sep 17 '09 12:09

cetnar


2 Answers

EDIT: Added a link to a blog post showing how to test Hibernate JPA with Spring and DbUnit.

[...] I'd like to have a maven build which create database from entities

There is a maven hibernate3 plugin with an hibernate3:hbm2ddl goal that might help. Coupled with the maven sql plugin, it should be possible to create this schema from the generated DDL.

[...] then populates initial/dictionary data

Again, the maven sql plugin could do the job here. Or maybe with DBUnit which is another elegant solution (see the maven dbunit plugin).

and run unit and integration tests.

Well, I'm not sure your unit tests should access the database but, for integrations tests, check DBUnit as I said. It's really a very nice tool that allows you to setup up the database in a known state, test tables for expected content after a test execution and put the database back in the initial state. See Testing JPA Hibernate with Spring & DbUnit for a nice example.

This process should be fully automated to put this build in CI server (hudson), crating new database from scratch is also appreciate.

I think this is feasible.

like image 130
Pascal Thivent Avatar answered Sep 30 '22 18:09

Pascal Thivent


@David, thanks for your post. The only reasonable solution I've find to seed initial data is similar to yours - that is inserting data in tests. In my code I even don't use hibernate3-maven-plugin. Database creation in done by Spring in file jpaContext.xml

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
  <bean
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="showSql" value="true" />
      <property name="generateDdl" value="true" />
      <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect"/>
  </bean>
</property>
<property name="dataSource" ref="dataSource"/>

As I wrote I use org.springframework.test.jpa.AbstractJpaTests with overriden method


  @Override
  protected String[] getConfigLocations() {
    return new String[]{
      "classpath:/jpaContext.xml"};
  }

I think it shold simplify your solution. I still search for a better resolution for creating initial data. Creating objects with a lot of relations by hand is cumbersome.

like image 43
cetnar Avatar answered Sep 30 '22 16:09

cetnar