Grails fixture, a great way to bootstrap your data

15 / Apr / 2011 by Partho Ghosh 4 comments

In my current project, I was given a task to bootstrap some data for our application. Initially I was thinking of doing it in the old fashion way, that is, writing a full blown CREATE data code for data creation like

Book myBook=new Book(name:'MyBook', publication:'lorem',price:'200')
Author author1=new Author(name:'Partho',country:'India')
author.addToBooks(myBook)

and so on.

But one of my colleagues(thank you Himanshu :) ) introduced me to this great plugin called fixtures for writing and creating the bootstrap data in more efficient and clean manner. It is really neat and intuitive. Fixture data is defined using a simple DSL that expresses objects, their properties and their relationships.A sample for how data is defined in fixtures is given below

myBook(Book){
     name=''MyBook";
    publication="lorem";
   price="200";
}

partho(Author){
name="Partho";
country="India";
books=[myBook];//That's how you create relationships in it.
}

No need to create multiple functions or services for bootstrapping. Just use your fixture files in your bootstrap.groovy (process explained below) or use your code inline. For data loading, we use a bean called fixtureLoader. An example would be

fixtureLoader.load("MyFixtureFile")

which helps you use a fixture file for bootstrapping.

For more information, please check out the fixture documentation over here and you can download the plugin from here. Its a really great plugin if you want your data to be loaded from anywhere in your application (bootstrap,tests,etc.). Do share your experience or anything cool you find out while using this plugin.Ciao

Regards,
Partho Ghosh

Tag -

FOUND THIS USEFUL? SHARE IT

comments (4)

  1. servletworld

    Does fixture plugin automatically save objects ?
    It doesnt seem to be working for me in integration tests

    Reply
  2. Leonid

    The fixtures is good but still not very good if you need to create a collection of domain objects. A lot of unnecessary “noise” is required to proper define a collection with supported DSL. Although it is more a shortcoming of Grails BeanBuilder DSL which is used by Fixtures.
    ObjectGraphBuilder class in Groovy and its Grails counterpart DomainBuilder look better for collections setup but the former does not help in creating proper GORM relationships and the second one has some issues in collections setup.

    Reply
    1. Partho Ghosh

      Thank you guys for sharing your experiences :)… I am a recent user of Grails fixtures and it sufficed our current needs. Yet to find any issues or hit some roadblocks :P But will keep in mind the issues faced by you guys. Thanks again for sharing.

      Reply
  3. Manuel

    Great plugin, but I had an issue loading data in a deployed app:
    I’ve finally managed to get my data loaded hacking at plugin’s source, but in a way that breaks logic forcing an application context when resolving fixture files.
    I tried to figure how to fix that bug “the right way”, but I got lost after some tries… :) but despite that bug I think that is a good plugin indeed!
    Ciao

    Reply

Leave a comment -