Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      Is there any reason why there are afterInsert, beforeInsert, afterUpdate, beforeUpdate but no afterSave or beforeSave in domain classes?

      I mean, most of the times I do something like:

      class MyDomainClass {
      def afterSave =

      { // some logic here }

      def afterInsert = afterSave, afterUpdate = afterSave
      }

      It would also be awesome if one could get access to the old properties in an afterSave/afterUpdate event.

      Also, I find it very limiting that we can't do something like that:

      class MyDomainClass {
      def afterUpdate()

      { new AnotherDomainClass(name: 'Something').save(flush: true) }

      }

      In several times I need the save operation to be finished before returning from afterUpdate. Also, in an afterSave of AnotherDomainClass, I would expect the instance of MyDomainClass to be persisted in the database.

      Is it possible for Grails to implement such helpful behavior?

      Thanks,

      Rodrigo.

        Activity

        Hide
        Graeme Rocher added a comment - - edited

        For your last problem. As described in the documentation, any new persistence logic in an even should be surrounded by withNewSession:

        class MyDomainClass {
            def afterUpdate(){ 
               withNewSession {
                     new AnotherDomainClass(name: 'Something').save(flush: true) 
               }
           }
        }
        
        Show
        Graeme Rocher added a comment - - edited For your last problem. As described in the documentation, any new persistence logic in an even should be surrounded by withNewSession: class MyDomainClass { def afterUpdate(){ withNewSession { new AnotherDomainClass(name: 'Something').save(flush: true ) } } }
        Hide
        Rodrigo Rosenfeld Rosas added a comment -

        In my case, the afterInsert of AnotherDomainClass needed to do a custom SQL query and assumed that the instance of MyDomainClass has already been persisted in database. Would that work as expected by using the withNewSession? I guess I've tried this at that time, but I can't be sure anymore as I changed my job recently...

        Show
        Rodrigo Rosenfeld Rosas added a comment - In my case, the afterInsert of AnotherDomainClass needed to do a custom SQL query and assumed that the instance of MyDomainClass has already been persisted in database. Would that work as expected by using the withNewSession? I guess I've tried this at that time, but I can't be sure anymore as I changed my job recently...
        Hide
        James Lang added a comment -

        agreed, the lack of beforeSave() and afterSave() are puzzling, must be a reason. Would be nice to see in 2.0

        Show
        James Lang added a comment - agreed, the lack of beforeSave() and afterSave() are puzzling, must be a reason. Would be nice to see in 2.0
        Hide
        Graeme Rocher added a comment -

        Burt - maybe you want to look at adding after/beforeSave

        Show
        Graeme Rocher added a comment - Burt - maybe you want to look at adding after/beforeSave

          People

          • Assignee:
            Unassigned
            Reporter:
            Rodrigo Rosenfeld Rosas
          • Votes:
            7 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Last Reviewed:

              Development