Tag Archives: grails

No signature of method addTo* is applicable

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.

Enabling Tomcat NIO Connector with Grails 2.0

Enabling Tomcat NIO Connector with Grails 2.0

At dozens of different sources, even in Graeme Rocher’s corresponding Jira ticket, it says that you need to put grails.tomcat.nio=true into your Config.groovy file. I believed that, too, and it drove me nuts to see that the org.apache.coyote.http11.Http11NioProtocol class wasn’t even instantiated.

I had to review the commit logs to figure out that the property should actually be in the BuildConfig.groovy file.

Now I finally got the log messages that I expected to see…

| Loading Grails 2.0.0
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application.....
| Running Grails application
| Enabling Tomcat NIO connector
http11.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8080"]
| Server running. Browse to http://localhost:8080/...

Using Grails without a database

Using Grails without a database

If you’re using Grails for a frontend without direct database access, there are four things you need to do.

  1. Remove Grails’ Hibernate plugin.
    grails uninstall-plugin hibernate
  2. Delete the datasource configuration file conf/DataSource.groovy
  3. Explicitly declare services as non-transactional. The default is true and your class methods would be proxied, but without the Hibernate plugin there is no transaction manager and the deployment will fail.
    class SomeService {
    	static transactional = false
    	// ...
    }
  4. Use command objects instead of domain objects, particularly if you need validation.
Deprecation of ConfigurationHolder in Grails 2

Deprecation of ConfigurationHolder in Grails 2

While porting my Grails applications to 2.0 RC1, I recognized that ConfigurationHolder is now deprecated. The new way to access the configuration is through dependency injection of the grailsApplication bean.

Before 2.0:

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
...
def value = CH.config.my.configEntry

With 2.0:

class MyService/MyController {
	def grailsApplication
	def method {
		def value = grailsApplication.config.my.configEntry
	}
}
Learning Grails the hard way: Facebook Authentication

Learning Grails the hard way: Facebook Authentication

Every now and then I try to explore a new programming language. Last year, I tried Scala, this year, I’m fiddling around with Grails. My first attempt was to try something I already implemented in other languages, like verifying Facebook’s signed_request. It’s fun to see what I tried to get it running and how a short version by a more experienced Groovy coder looks like.

Compare both versions after the break if you like.