2009年2月7日土曜日

[Grails]can't override the methods(like save(), find()...) defined in domain class

References
  • Grails Documentation
    http://grails.org/doc/1.0.x/
Description
  • Can't override the standard methods (like the save(), find(), delete()..) and dynamic finders defined in domain class. For example, I define the original save() method into the domain class and start the grails jetty, System ignores my original save() method and overrides the save() method.
Cause
  • According to the doWithDynamicMethods() method defined in HibernateGrailsPlugin.groovy file, the Clusure named "lazyInit" is defined, and this closure is called from the methodMissing methods.
    When calling the methodMissing methods, System tries to initialize the domain class and inject various dynamic methods into the domain class.
    When System initializes the Domain Class and calls the dynamic method at the first time,
    Groovy calls the methodMissing method if System can't find the method definition in the domain class. And then, System injects and overrides the pre-defined and duplicated methods.
    Please refer to the org.codehaus.groovy.grails.plugins.orm.hibernate.HibernateGrailsPlugin in source code.
Resolution
  • Change the method name like save()==>saveXXX(), we have to avoid to duplicate the method name(or change and override the methodMissing method definition?).

0 件のコメント: