Blog, Software Engineering

No signature of method addTo* is applicable

Last night, I started getting an exception in one of my pet projects, created with Grails:
No signature of method: package.Parent.addToChildren() is applicable for argument types: (package.Child) values: [package.Child : null]
Possible solutions: getChildren(). Stacktrace follows:
groovy.lang.MissingMethodException: No signature of method: package.Parent.addToChildren() is applicable for argument types: (package.Child) values: [package.Child : null]

I searched for the problem, read dozens of different hints regarding proper general ORM one-to-many setups, GORM-specific hasMany/belongsTo relationships, cascading, saving before adding, etc. All were different problems leading to the same exception.
My problem, however, was far more trivial. Here’s the code that made me want to tear my hair out.

class Parent {
	static hasMany = {
		children: Child
	}
}

Do you spot the mistake?
I used curly braces for the hasMany relationship. Groovy doesn’t complain at all, as it’s valid syntax. But to make GORM work the expected way, it should be square brackets:

class Parent {
	static hasMany = [
		children: Child
	]
}

I’ve lost two hours of my life to this one.

2 Comments

Comments are closed.