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.
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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With