1
votes

I am trying to use the Class.forName('com.mypack.MyDomain').newInstance() to create an instance of the grails domain from its qualified name. But Its throwing a ClassNotFoundException. I assume this is because the .forName('') expects the class to be a java class instead of a groovy class? How to make this work in grails, or is there another method to create the domain object from the class name in String format.

Thanks Priyank

2

2 Answers

8
votes

Another route to try would be to do:

GrailsDomainClass dc = grailsApplication.getDomainClass( 'com.mypack.MyDomain' )
def newDomainObject = dc.clazz.newInstance()
1
votes

Try

GrailsClass clazz =  grailsApplication.getArtefactByLogicalPropertyName(DomainClassArtefactHandler.TYPE, className)
clazz.clazz.newInstance()