views:

820

answers:

6

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?

Partial solution 2010-03-19

Suggested alternatives are:

  • Using unit tests to populate the database #2423663
  • Using ServletContextListener to gain control on web context startup #2424943 and #2423874
  • Using Spring ApplicationListener and Spring's Standard and Custom Events #2423874

I implemented this using Spring's ApplicationListener:

Class:

public class ApplicationContextListener implements ApplicationListener {

    public void onApplicationEvent(ApplicationEvent event) {

        if (event instanceof ContextRefreshedEvent) {
            ...check if database is already populated, if not, populate it...
        }
    }
}

applicationContext.xml:

<bean id="applicationContextListener" class="my.namespaces.ApplicationContextListener" />

For some reason I couldn't get ContextStartedEvent launched, so I chose ContextRefreshedEvent which is launched in startup as well (haven't bumped into other situations, yet).

How do I flush the database? Currently, I simply remove HSQLDB artifacts and a new schema gets generated on startup by Hibernate. As the DB is then also empty.

+1  A: 

Depend on your db. It is better to have script to set up db

Artic
I don't quite understand what you mean by this. JPA/Hibernate are supposed to be database agnostic. Yes, it depends on the products how to set them up and I have functional Spring configuration. However, I don't know how to launch that configuration and execute a certain class/method using Maven tasks. My database is HSQLDB but it could be any. Can you give an example?
Tuukka Mustonen
+2  A: 

You can write a unit test to populate the database, using JPA and plain Java. This test would be called by Maven as part of the standard build lifecycle. As a result, you would get an fully initialized database, using Maven, JPA and Java as requested.

Olivier Croisier
I suppose this initialization would execute every time I restart/redeploy my server? I want to manually execute the flush/population. Also, do you really consider molesting unit tests a better approach than using a servlet?
Tuukka Mustonen
Well, you wanted to be part of the Maven lifecycle, so I suggested this approach. But if this requirement can be lifted, the unit test is definetly not what I'd recommend.You could do that with a Servlet as you suggest, of with a ServletContextListener if you want the DB to be automatically populated on application start.BTW, if you use a Servlet, remember to setup some sort of security to avoid random people to mess with your DB...
Olivier Croisier
+1  A: 

The usual way to do this is to use a SQL script. Then you run a specific bash file that populate the db using your .sql
If you want to be able to programmatically set your DB during the WebApp StartUp you can use a Web Context Listener.
During the initialization of your webContext you can use a Servlet Context Listener to get access to your DAO (Service Layer.. whatever) create your entities and persist them as you use to do in your java code

p.s. as a reference Servlet Life Cycle

If you use Spring you should have a look at the Standard and Custom Events section of the Reference. That's a better way to implement a 'Spring Listener' that is aware of Spring's Context (in the case you need to retrieve your Services form it)

al nik
So I could do the population/flush when ContextStartedEvent was launched - does having Spring context up really mean, that all the other contexts (e.g. Hibernate) have been succesfully initialized? If yes, then I think this is the way to go. In order to prevent flush/population after each restart, I could first run the application with an additional variable passed to maven/JVM and use that variable as an indicator whether or not to do the actions. How would you automatically shut down the process after flush/population was executed? Use brute force to kill the process?
Tuukka Mustonen
Yes, a variable passed in at startup should be fine. It's a listener that is used when the context is started, you don't need to kill any process. It's easier to access the bean defined in your spring context etc. Your Spring xml config should be ok when you reach ContextStartedEvent.. It's the 1st place I would put my logic to persist real entities using the Spring services.. it should be easy and fast to develop.. give it a try
al nik
+1  A: 
  1. In the aforementioned ServletContextListener or in a common startup place put all the forthcoming code
  2. Define your data in an agreeable format - XML, JSON or even java serialization.
  3. Check whether the initial data exists (or a flag indicating a successful initial import)
  4. If it exists, skip. If it does not exist, get a new DAO (using WebApplicationContextUtils.getRequiredWebApplicationContext().getBean(..)) , iterate all predefined objects and persist them via the EntityManager in the database.
Bozho
A: 

You could create JPA entities in a pure Java class and persist them. This class could be invoked by a servlet but also have a main method and be invoked on the command line, by maven (with the Exec Maven Plugin) or even wrapped as a Maven plugin.

But you're final workflow is not clear (do you want the init to be part of the application startup or done during the build?) and requires some clarification.

Pascal Thivent
Sorry for the delayed reply. I suppose I want the population to occur during the application startup (or during run by executing a servlet) in order to flexibily flush/populate the db. I took a glance at the Maven exec plugin, but I fear it cannot set up the Spring context. Also, directly running the class doesn't set up Spring context either, so I guess I would need special code to programmatically set it up.
Tuukka Mustonen
A: 

I'm not sure if you can get away from using some SQL. This would depend if your develoeprs are staring with an empty database with no schema defined or if the tables are there but they are empty.

If you starting with empty tables then you could use a Java approach to generating the data. I'm not that familiar with Maven but I assume you can create some task that would use your DAO classes to generate the data. You could probably even write it using a JVM based scripting language like Groovy that would be able to use your DAO classes directly. You would have a similar task that would clear the data from the tables. Then your developers would just run these tasks on the command line or through their IDE as a manual step after checkout.

If you have a fresh database instance that I think you will need to execute some SQL just to create the schema. You could technically do that with executing SQL calls with hibernate but that really doesn't seem worth it.

David W Crook
What you suggest is quite what I need. Having simple tasks re-initialize database would rock and that's actually what I am here after (I would like them to be Maven tasks, but sure, they could be any). However, I think it's still not clear for both of us how to accomplish this. That was the core of the question. If you got any samples maybe you could share? By the way, we don't actually need any raw SQL/schema setup - Hibernate does that for us.
Tuukka Mustonen