pri_dev
pri_dev

Reputation: 11665

creating a grails domain class from its class name

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

Upvotes: 1

Views: 3696

Answers (2)

tim_yates
tim_yates

Reputation: 171194

Another route to try would be to do:

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

Upvotes: 8

Sudhir N
Sudhir N

Reputation: 4096

Try

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

Upvotes: 1

Related Questions