Handling corrupted references through ignoreNotFound database mapping in Grails

17 / Apr / 2012 by Tarun Pareek 0 comments

Hi,
 
Recently i had a use-case where we have the legacy database and it contains the corrupted references of a non existent record in many-to-one relationship, and i have to populate a grid that contain info also from referenced record. As referenced record doesn’t exist, So when we refer to the certain record will result in Hibernate throwing exception :

org.hibernate.ObjectNotFoundException

and not a single record loaded on grid.
 
As it is legacy database i am not sure what should be the correct value for the particular record.
 
After few searches, i come accross a property in grails database mapping to handle such cases, let see how it work :

class Task{
   String name
   ParentTask parent

   static mapping = {
       parent ignoreNotFound: true
   }
}

class ParentTask{
   String name
}

Basically, ignoreNotFound simply mapping hibernate not-found property.
 
Note : A record loaded with ignoreNotFound: true will throw an exception during call of save() because of the missing reference.
 
Either you can first update the invalid property or corrupted reference to the correct reference and then update other property that will solve your problem. It depend how you handle such scenario on your usecase.
 
Thanks,
Tarun Pareek
tarun@intelligrape.com

More Blogs by Me
 

FOUND THIS USEFUL? SHARE IT

Leave a comment -