PDA

View Full Version : beforeInsert return false -> object saved



Saioa
Feb 4th, 2011, 03:38 AM
Hello,

In my domain class I've created the beforeInsert method and I'm checking the value of a property there. In some cases I don't want the object to be persisted to the db, so I'm returning false. I've read that this is supposed to work but it doesn't, the object is still being saved...

This is my method

def beforeInsert = {
if (accion=="update")
{
QuartzJob tareaBorrado = new QuartzJob(idEmpresa:idEmpresa,idSede:idSede,url:ur l,accion:'delete')
tareaBorrado.save()
QuartzJob tareaCrawl = new QuartzJob(idEmpresa:idEmpresa,idSede:idSede,url:ur lNueva,accion:'crawl')
tareaCrawl.save()
return false
}
else if (accion=="schedule")
{
Calendar calendario = new GregorianCalendar()
calendario.set(calendario.get(Calendar.YEAR),calen dario.get(Calendar.MONTH),calendario.get(Calendar. DATE),grailsApplication.config.crawler.reindexacio n.hora.toInteger(),grailsApplication.config.crawle r.reindexacion.minutos.toInteger())
fechaSchedule = calendario.getTime()
dateCreated = calendario.getTime()
}

}

What I want to achieve is to change an insert wich has the property accion="update" to two inserts, one with accion="delete" and another one with accion="crawl". The rows with accion="delete" and accion="crawl" are inserted without problem, but the row with accion="update" is also persisted.
Any suggestion?
Thanks

pledbrook
Feb 8th, 2011, 08:15 AM
This isn't supported for beforeInsert, only beforeUpdate. A missing feature I guess. Not sure if there is a suitable workaround - you'd need to look into what support Hibernate has for this kind of thing. Perhaps a custom event listener?

Saioa
Feb 8th, 2011, 08:55 AM
Ok, I'll check Hibernate or try to find another workaround.

Thanks!