Quantcast

better object persistance than through save?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

better object persistance than through save?

Sven Prevrhal
Hi, there must be a standard solution to this: I'd like to first see
whether a particular object already exists, i.e. use find by example.
If it is, I'd like to continue with the one that was found. If isn't
I'd like to try and save it. I'm thinking of using something like


        Object.metaClass.safesave = {
            def o = delegate.class.find(delegate)
            if (!o) {
                if (delegate.validate()) {
                    try {
                        o = delegate.save()
                    }
                    catch (Exception ex) {
                        println ex.getMessage()
                        delegate.errors.allErrors.each {
                            println it
                        }
                    }
                } else {
                    delegate.errors.allErrors.each {
                        println it
                    }
                }
            }
            o
        }

Does that make sense or is there a better solution?

Cheers, Sven

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: better object persistance than through save?

pledbrook
> Does that make sense or is there a better solution?

Such a findOrCreate() method has been proposed for 1.4 although I
don't think there is an issue for it yet. Would be worth raising a
JIRA and setting target version to 1.4 along with your suggested
implementation.

Peter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...