GORM events in Grails 1.0

Date February 17, 2008

I was doing a big of Grails work this weekend and stumbled on this page describing GORM events.  The 1.0 release of Grails introduced 4 new events in to domain objects:

  • beforeInsert
  • beforeUpdate
  • beforeDelete
  • onLoad

You can probably guess what these do based on the name - there’s nothing terribly secret going on here.  It’s not even terribly earth-shattering, but a welcome addition to have these sorts of things as defined standards.  Is there any use to have ‘after’ events?  afterInsert() would be a “nice to have” because, from a logging standpoint, you’d be able to log whatever sequence number was assigned.  In a pure ’sequence’-based db, you could know ahead of time by grabbing the next sequence number and using that.  In MySQL and MSSQL, you have the ‘auto-increment’ approach, where you don’t have the ID assigned until after the insert is done.

Also, that same page notes that the GORM system will automatically fill in your “dateCreated” and “lastUpdated” times in the database if those fields exist.  You don’t have to do a thing.  Well, for the ‘updated’ date you do need to add a constraint to the domain defining the lastUpdated property as nullable, because on the first insert the updated date is still null (hasn’t technically been updated yet).  Again, this is not terribly ground-breaking, but having this as a defined standard in the GORM world gives another ‘best practice’ to rally around.

 Did you like this post? Buy me a hot chocolate!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">