If i have a form such as bellow
<input type="text" name="someCollection[0].someAssociation.id"/>
<input type="text" name="someCollection[1].someAssociation.id"/>
<input type="text" name="someCollection[2].someAssociation.id"/>
Because Grails provides automatic data binding when it encounters a .id suffix, it seems Grails uses get method instead of load one which implies 3 queries as follows
SELECT * FROM SomeAssociationClass WHERE id = ?
SELECT * FROM SomeAssociationClass WHERE id = ?
SELECT * FROM SomeAssociationClass WHERE id = ?
I just need load method because it does not hit the database unless you use other than getId() (NOT APPLIED). So, how can i customize data binding so that it uses load method ?
To avoid loading an instance of the database, we need to remove the .id suffix and register a custom propertyEditor which takes care of invoking load method
class AppPropertyEditorRegistrar implements PropertyEditorRegistrar {
void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Question, new PropertyEditorSupport() {
@Override
void setAsText(String value) {
setValue(Question.load(value as Long))
}
})
}
}