tags:

views:

35

answers:

2

I know I can create a class using: grails create-domain-class book

This will create a class file in my project that I can then access via the console.

My question is 'how can I create a use a class in the console itself that I can then save()?'

If I type the following in the console I get an error:

  class Tree {
    String name
    Date dateCreated
  }

  def tree = new Tree(name:'oak').save()

The error I get is:

   Exception thrown

   groovy.lang.MissingMethodException: No signature of method: 
       Tree.save() is applicable for argument types: () values: []
   Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure),
        sleep(long), use([Ljava.lang.Object;)

    at ConsoleScript6.run(ConsoleScript6:6)
+1  A: 

This isn't possible. You just created a plain class which the console compiles for you and makes available, but there's no way to transform it to a domain class and give it GORM behaviors.

Domain classes need to be in the grails-app/domain folder or can also be classes mapped with Hibernate hbm.xml files or annotations.

Burt Beckwith
Bill Comer
A: 

This is not true, you CAN test domain classes directly from Grails console -- just start your console script with "import package.domain-class-name"

If you have a domain class in package test:

class A {
String testStr
}

then create console script like so:

import test.A
def t = new A(testStr:"blah").save()

// works!

enjoy...

MVC You Know Me
I know I can do that, but that is not what I asked. I was asking if I can create the domain class actually in the console ans so do true prototyping with the guarantee of leaving no clutter behind afterwards.
Bill Comer
Agreed, would be nice, very nice, useful, etc., but not yet the case. With grails interactive console is relatively snappy (i.e domain classes refresh quickly on code change) so you can prototype, just more slowly than you (and everyone else) would like.
MVC You Know Me