Search code examples
mysqlhibernategrailsgrails-orm

How to cope with broken relationships in Grails/GORM?


I have a grails 2.4.3 application going to an old, crusty database with data of questionable integrity.

Over time, various broken relationships have built up...don't know why, I/owning organisation are prepared to say "that's just life/history and we'll live with it."

The question is, how to just "live with it"?

Specifically: how to cope with these broken relationships in Grails/GORM?

At the moment, when I access a Note instance with a broken relationship, I get an exception:

Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [Contract#292]

I probably COULD spatter tests like:

Contract.exists(note.contract_id)

all throughout my code but...YUK! And this would become a massive exercise: checking every relationship prior to use would be HORRID.

Is there a better way? A way that keeps my code both DRY and more robust?

This is not particularly a GORM/Hibernate/ORM-specific issue but I am wondering if the frameworks bring anything to the table to help with this.

The relationships are mostly marked nullable: true.

My database is mysql/innodb.


Solution

  • You could specify ignoreNotFound: true in the mapping block for the relationship.

    http://grails.org/doc/latest/ref/Database%20Mapping/ignoreNotFound.html