GORM Quirks and Serialized Solutions

I have been working on a few projects at VTTI that use Groovy/Grails and the GORM. Recently I ran into the need to store a map of lists in such a way that the user could edit it, but it would also persist between shutdowns of the application. If not, a ton of “response types” and “event types” would have to be recreated. So there are two obvious options for saving this information, either write it to a file, or persist it to the database. Since IT dislikes extra files and folders mucking about, I decided it would be best to just keep everything in the database because I could. I figured, “GORM will just take care of my objects and build all the tables and set everything up nicely.” That is until I threw it a domain class with a map of lists I wanted persisted to the database. That launched me into a quest of how to solf my data structure conversion issue!

The Issue -

After lots of debugging and testing, I realized that you can’t do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Names {

/**
* This is a domain class and it holds a map of retired named storms for the last few years
* we will use GORM to give use basic CRUD
**/

Map years

Names () {
years = [2004:['GASTON', 'MATTHEW', 'OTTO'], 2003:['PETER', 'ODETTE', 'NICHOLAS', 'MINDY', 'LARRY'], 2002:['HANNA', 'FAY', 'CRISTOBAL']]
}
static constraints = {
}
...
...

}

Because when GORM goes to convert the object into something it can persist in the database, it runs into an ArrayList error. Apparently there is not a suffuicent toString() method for the ArrayList class.

A Solution -

After searching around for a solution, and thinking of possibly changing data-structures, I finally came accross serialization through a coworker. He sugguested I sereialize the object to either JSON or XML and then save the resulting string to the database. That way I can just fetch the string and parse out the lists of names.

After wrapping the needed methods into two helper functions, I am easily able to go to and from the database and JSON to populating select fields and providing an admin interface to allow for dynamic expantion of the map and list value pairs.

Example Code -

There are two functions that helped me convert between a map of JSON strings and a map of lists:

1
2
3
4
5
6
Map export(names){
    for (i in names){
        i.value = groovy.json.JsonOutput.toJson(i.value)
    }
    return names
}

and

1
2
3
4
5
6
7
Map import(names) {
    def slurper = new groovy.json.JsonSlurper()
    for (i in names) {
        i.value = slurper.parseText(i.value)
    }
    return names
}

Both of these classes (JsonOutput and JsonSluper) are availible through the groovy.json package. Hopefully with your new found knowledge of how to use JSON and Groovy togeather to persist datastructures and objects to the database to build new and excited applications you thought never possible, or at least too much effort to do right.
 

 

There are 2 Comments to "GORM Quirks and Serialized Solutions"

  • Excellent site yοu have here.. It’s hard to find quality writing like yours these days. I really appreciate individuals like you! Take care!!

  • I have learn a few just right stuff here. Certainly price bookmarking
    for revisiting. I surprise how so much effort you set to
    make any such fantastic informative web site.

Write a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>