Ever wanted to see if a domain class had a many-to-many association in a Grails controller? What about getting a list of persistent properties? In this post, I will outline a quick solution that Jakob Külzer and I figured out for a recent project…
The secret to accessing Grails domain class properties outside of a domain class is to use the DefaultGrailsDomainClass.
Let’s say I have a domain object Foo, In the FooController, I can call
def fooDomain = new DefaultGrailsDomainClass( Foo.class )
then I can call
fooDomain.properties.each{
println “${it.name} ${it.oneToMany}”
}
Which will show me all the properties for this domain class and whether the result is a one to many association.
You can access the full API here : DefaultGrailsDomainClass
Use carefully!
