Travis Webb
Travis Webb

Reputation: 15018

How to persist Closure in Database in Grails?

I am trying to persist a Groovy Closure (which are allegedly serializable) as a property of one of my Grails domain objects. Currently I'm basically doing this:

class MyClass {
    ....
    Closure myClosure
    static mapping = { myClosure size: 1024 * 1024, type: 'blob' }
}
new MyClass(myClosure: { ... do some stuff .. }.dehydrate()).save()

I tried changing 'blob' to 'binary' but that doesn't work. I get an error similar to: context.GrailsContextLoader Error executing bootstraps: BootStrap$_obj_closure3 cannot be cast to java.sql.Blob

How should I set up my domain object in order to be able to store a closure?

I am using Grails 2.1.1 with Groovy 2.0

Upvotes: 2

Views: 833

Answers (1)

Travis Webb
Travis Webb

Reputation: 15018

I needed this:

static mapping = {
    myClosure sqlType: 'blob'
}

Upvotes: 3

Related Questions