Grails
  1. Grails
  2. GRAILS-5725

Allow plugins to easily insert hibernate event listeners

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1.2
    • Fix Version/s: 1.3.4
    • Component/s: Persistence, Plugins
    • Labels:
      None
    • Patch Submitted:
      Yes

      Description

      When trying to create a plugin for Hibernate Envers, I had issues finding the correct way to plug the hibernate event listeners in the SessionFactory.

      The solution usually found on the web involves mucking with the SessionFactory after it is built to add more event listeners.

      This works quite often but not with envers because its event listener implements Initializable and thus must be set on the configuration object.

      I have seen the related issue GRAILS-4651 but an Interceptor is not enough for envers.

      There is also GRAILS-5453, but my goal was to simplify the addition of events in a plugin not directly in the application. However I may look at creating a patch for GRAILS-5453 also.

      Attached is a patch against 1.1.2 (and a new file to add in grails/src/persistence/org/codehaus/groovy/grails/orm/hibernate)

      With this patch applied I can do this in a plugin :

      def doWithSpring = {
      		auditListener(AuditEventListener)
      		hibernateEventListeners(HibernateEventListeners) {
      			listenerMap = ['post-insert':auditListener,
      			               'post-update':auditListener,
      			               'post-delete':auditListener,
      			               'pre-collection-update':auditListener,
      			               'pre-collection-remove':auditListener,
      			               'post-collection-recreate':auditListener]			
      		}
      }
      
      1. grails-events.patch
        12 kB
        Michel Rasschaert
      2. HibernateEventListeners.java
        0.3 kB
        Michel Rasschaert

        Issue Links

          Activity

          Hide
          Michel Rasschaert added a comment -

          Is there a chance that the patch might be accepted if it was updated to the latest grails version ?

          Show
          Michel Rasschaert added a comment - Is there a chance that the patch might be accepted if it was updated to the latest grails version ?
          Hide
          Graeme Rocher added a comment -

          Sure, if you update it to the latest version I'll get it applied

          Show
          Graeme Rocher added a comment - Sure, if you update it to the latest version I'll get it applied
          Show
          Burt Beckwith added a comment - http://github.com/grails/grails-core/commit/657bad4f7bde72fd67f23701e2caa27ba6593157
          Hide
          Michel Rasschaert added a comment -

          Thanks Burt for updating the patch and applying it. I'm not as proficient with git as I'd like to be (yet)

          Show
          Michel Rasschaert added a comment - Thanks Burt for updating the patch and applying it. I'm not as proficient with git as I'd like to be (yet)
          Show
          Burt Beckwith added a comment - Added docs: http://github.com/grails/grails-doc/commit/261854727cfbaa5bf4b16da0e3a4b055251bc75e
          Hide
          Kim A. Betti added a comment -

          Perhaps there should be an api for adding (and removing?) event listeners? This solution is a bit brittle. If you register this bean in your application and a plugin, the plugin's bean will silently be replaced. I guess the same would happen if multiple plugins in an application tries to register this bean.

          Show
          Kim A. Betti added a comment - Perhaps there should be an api for adding (and removing?) event listeners? This solution is a bit brittle. If you register this bean in your application and a plugin, the plugin's bean will silently be replaced. I guess the same would happen if multiple plugins in an application tries to register this bean.
          Hide
          Serge P. Nekoval added a comment -

          I agree. Ideally it should be able to detect multiple HibernateEventListeners in the ApplicationContext and attach all of them.

          Show
          Serge P. Nekoval added a comment - I agree. Ideally it should be able to detect multiple HibernateEventListeners in the ApplicationContext and attach all of them .

            People

            • Assignee:
              Burt Beckwith
              Reporter:
              Michel Rasschaert
            • Votes:
              5 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development