Hibernate events plugin

On a current grails project I had a need to act on my domain classes using hibernate’s persistence lifecycle. However, it turns out that grails only provides three lifecycle methods: beforeInsert, beforeUpdate, beforeDelete. Four if you count onLoad. I was really interested in having an afterInsert method so that I could save a file to the filesystem after it’s metadata had been saved to the database. So I used the hibernate event system to round out the lifecycle methods with seven more hooks:

  • afterInsert
  • afterUpdate
  • afterDelete
  • beforeLoad
  • afterLoad
  • beforeSave
  • afterSave

So if a domain model contains a closure with any of these names they will be called during their turn in the lifecycle. I think they are mostly self explanatory except for before/afterSave. The purpose for these methods is to be called when the persistence call is either an insert or an update. So if you don’t care if the object is being saved for the first time or being updated you can use this method.

Unfortunately the plugin uses a hibernate.cfg.xml, because it is the only way you can participate in the creation of the SessionFactory.

I haven’t decided if I will release the plugin yet. Maybe if there is interest.

Update: Ok, so some interest was expressed. Here it is: http://thegioraproject.com/files/grails/plugins/grails-hibernate-events-0.1.zip. Feedback is always welcome.

Update 2: I’ve added a doc page to the grails wiki.  Not really much is needed, but it could clear things up a bit.  http://docs.codehaus.org/display/GRAILS/Hibernate+Events+Plugin

8 Responses to “Hibernate events plugin”

  1. Shawn Hartsock Says:

    Oh, I’d say there’s some interest. I’ve got to do exactly this on my current project. I was either going to cop-out and back it off to in-database triggers or I was going to essentially invent this plugin… small world.

  2. Sakuraba Says:

    Publish it! I definetly looks interesting and IMO should be part of core Grails and not a separate plugin. Maybe Graeme comes up with an idea on how to avoid the second hibernate.cfg.xml file. Some HB-aquivalent to the doWithApplicationContext = {…} closure could be appended for 1.1 or something like that.

  3. Kevin Burke Says:

    Shawn: Small indeed. I’ve uploaded the zip if you’re interested.

    Sakuraba: The problem with the SessionFactory is that it’s configuration is immutable. So once it is configured it is set in stone. Which only means that plugins that contribute to its creation would have to be loaded prior to the core plugins. The only other way that I could think of is putting a closure in Config.groovy that could be called from within the core HibernateGrailsPlugin sessionFactory builder. In any case this would be very useful as there are quite a few options that Spring’s LocalSessionFactoryBean exposes that people might want to take advantage of.

  4. Burt Beckwith Says:

    You can programmatically add listeners at any time. I have code in Bootstrap.groovy that does this. I could have put them in hibernate.cfg.xml, but I have one for each event (21 event types) so I use reflection to register them. You just have to be careful to add to the listener arrays - there’s no addListener() methods, only getters and setters for the whole arrays, so you need to add your listener(s) to the array. You can remove the default listener, and also since you’re setting the array you can decide the order.

    Here’s an example for PostInsertEvent/PostInsertEventListener:

    import org.hibernate.event.EventListeners

    EventListeners eventListeners = sessionFactory.eventListeners
    def listeners = eventListeners.postInsertEventListeners
    listeners = Arrays.copyOf(listeners, listeners.length + 1)
    listeners[-1] = new MyPostInsertEventListener()
    eventListeners.postInsertEventListeners = listeners

  5. Sakuraba Says:

    Kevin:

    Put a thread an on the mailing list about this. I am quite sure that Graeme is interested to discuss this idea. I definetly think this functionality should make it into 1.1 ;)

  6. Kevin Burke Says:

    Burt: That’s a good point. I saw that in the hibernate docs, but for some reason completely ignored it. I think because I saw how much easier it is to register using the LocalSessionFactoryBean in Spring, and put all my efforts there. Thanks, I think I’ll use that.

    Sakuraba: If I get a minute I will.

  7. thegioraproject.com » Blog Archive » Hibernate events plugin v0.2 Says:

    […] About the Project « Hibernate events plugin […]

  8. Ursula Maldonado Says:

    9gayclw0oqza3a97

Leave a Reply