How to populate web application with initial data

血红的双手。 提交于 2019-12-05 17:43:07

问题


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 wich 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), crating 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.


回答1:


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.




回答2:


@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.




回答3:


I solved a problem nearly identical to yours by extending

org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests

Then I used the hibernate3-maven-plugin to populate the database (I used hsqldb) at test time:

      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <components>
                    <component>
                        <name>hbm2ddl</name>
                        <implementation>jpaconfiguration</implementation>
                    </component>
                </components>
                <componentProperties>
                    <drop>true</drop>
                    <jdk5>true</jdk5>
                    <propertyfile>target/classes/jdbc.properties</propertyfile>
                    <skip>${maven.test.skip}</skip>
                </componentProperties>
            </configuration>
            <executions>
                <execution>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>hbm2ddl</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>${jdbc.groupId}</groupId>
                    <artifactId>${jdbc.artifactId}</artifactId>
                    <version>${jdbc.version}</version>
                </dependency>
            </dependencies>
        </plugin>

I posted a simple maven project at Google Code to demonstrate the technique.



来源:https://stackoverflow.com/questions/1438606/how-to-populate-web-application-with-initial-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!