1

In my app I have a grails domain class Student. I also have a few classes in src.java originally written in plain java.

In one of those classes in some method I am trying to get a collection of students like this:

this.students = new ArrayList<Student>(Student.findAll());

Grails package is imported, intellij doesn't complain, however I get foolwing error while trying to compile.

[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  [groovyc] Compile error during compilation with javac.
  [groovyc] F:\path\ProjectName\src\java\grb\StudentSchedule.java:58: error: cannot find symbol
  [groovyc]         this.students = new ArrayList<Student>(Student.findAll());
  [groovyc]                                                       ^
  [groovyc]   symbol:   method findAll()
  [groovyc]   location: class Student

I have been also trying different methods, on Student, but all give me the same error - cannot find symbol.

4
  • 2
    The method findAll() is dynamically added by Grails to all domain classes. So calling the method relies on Groovy's dynamic dispatch to find the method and then execute it. Because the method doesn't exist in the class itself, you can't call it directly from Java. Commented Apr 24, 2016 at 14:30
  • Emmanuel, thanks for the comment! Is there any way to workaround? Commented Apr 24, 2016 at 14:43
  • Write those classes in groovy instead of Java Commented Apr 24, 2016 at 15:03
  • Which version of Grails are you using? Commented Apr 24, 2016 at 15:44

1 Answer 1

4

As the Grails docs state: You can also write Grails domain classes in Java
You have to use org.codehaus.groovy.runtime.InvokerHelper from Java
For more information see this mailing list
Kind of sample code:

import my.package.User
import org.codehaus.groovy.runtime.InvokerHelper;

List allInstances = (List)InvokerHelper.invokeMethod(User.class, "list", null)); 

User one=(User)InvokerHelper.invokeMethod(User.class, "get", id);

maybe this post can help as well.
But I would agree on the idea of writing the classes in Groovy.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mikelis! That is what I ended up doing, and it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.